给定一个由2的N 个幂组成的数组arr[] ,任务是计算对(arr[i], arr[j]) 的数量,使得i < j并且arr[j]可以被arr[i]整除.
例子:
Input: arr[] = {4, 16, 8, 64}
Output: 5
Explanation:
The pairs satisfying the given condition are {4, 16}, {4, 8}, {4, 64}, {16, 64}, {8, 64}.
Input: arr[] = {2, 4, 8, 16}
Output: 6
Explanation:
The pairs satisfying the given condition are {2, 4}, {2, 8}, {2, 16}, {4, 8}, {4, 16}, {8, 16}.
朴素的方法:最简单的方法是生成给定数组arr[]的所有对,对于每一对,检查arr[j] % arr[i]是否为0 。如果发现为真,则将count加 1。最后,在检查所有对后打印count的值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:为了优化上述方法,该想法基于以下观察:任何2 的幂在其二进制表示中只有一个设置位。对于任何此类元素arr[j] ,其设置位位于小于或等于arr[j]设置位位置的所有 2 的幂将满足给定条件。请按照以下步骤解决问题:
- 初始化大小等于31的辅助数组setBits ,并将count初始化为0以存储所需对的数量。
- 使用变量i遍历数组arr[]并执行以下操作:
- 将arr[i]最左边的设置位存储在bitPos 中。
- 使用变量j在范围[0, bitPos] 中迭代,并在每一步中通过setBits[j]递增计数。
- 将setBits[bitPos]增加1 。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// pairs as per the given conditions
void numberOfPairs(int arr[], int N)
{
// Initialize array set_bits as 0
int set_bits[31] = { 0 };
// Store the total number of
// required pairs
int count = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Store arr[i] in x
int x = arr[i];
// Store the position of the
// leftmost set bit in arr[i]
int bitpos = -1;
while (x > 0) {
// Increase bit position
bitpos++;
// Divide by 2 to shift bits
// in right at each step
x /= 2;
}
// Count of pairs for index i
// till its set bit position
for (int j = 0;
j <= bitpos; j++) {
count += set_bits[j];
}
// Increasing count of set bit
// position of current elelement
set_bits[bitpos]++;
}
// Print the answer
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 4, 16, 8, 64 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
numberOfPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count the number of
// pairs as per the given conditions
static void numberOfPairs(int arr[], int N)
{
// Initialize array set_bits as 0
int []set_bits = new int[31];
Arrays.fill(set_bits, 0);
// Store the total number of
// required pairs
int count = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++)
{
// Store arr[i] in x
int x = arr[i];
// Store the position of the
// leftmost set bit in arr[i]
int bitpos = -1;
while (x > 0)
{
// Increase bit position
bitpos++;
// Divide by 2 to shift bits
// in right at each step
x /= 2;
}
// Count of pairs for index i
// till its set bit position
for (int j = 0;
j <= bitpos; j++)
{
count += set_bits[j];
}
// Increasing count of set bit
// position of current elelement
set_bits[bitpos]++;
}
// Print the answer
System.out.println(count);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 4, 16, 8, 64 };
int N = arr.length;
// Function Call
numberOfPairs(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Python3
# Python3 program for the above approach
# Function to count the number of
# pairs as per the given conditions
def numberOfPairs(arr, N):
# Initialize array set_bits as 0
set_bits = [0]*31
# Store the total number of
# required pairs
count = 0
# Traverse the array arr[]
for i in range(N):
# Store arr[i] in x
x = arr[i]
# Store the position of the
# leftmost set bit in arr[i]
bitpos = -1
while (x > 0):
# Increase bit position
bitpos += 1
# Divide by 2 to shift bits
# in right at each step
x //= 2
# Count of pairs for index i
# till its set bit position
for j in range(bitpos + 1):
count += set_bits[j]
# Increasing count of set bit
# position of current elelement
set_bits[bitpos] += 1
# Prthe answer
print (count)
# Driver Code
if __name__ == '__main__':
arr = [4, 16, 8, 64]
N = len(arr)
# Function Call
numberOfPairs(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the number of
// pairs as per the given conditions
static void numberOfPairs(int[] arr, int N)
{
// Initialize array set_bits as 0
int []set_bits = new int[31];
for (int i = 0; i < N; i++)
{
set_bits[i] = 0;
}
// Store the total number of
// required pairs
int count = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++)
{
// Store arr[i] in x
int x = arr[i];
// Store the position of the
// leftmost set bit in arr[i]
int bitpos = -1;
while (x > 0)
{
// Increase bit position
bitpos++;
// Divide by 2 to shift bits
// in right at each step
x /= 2;
}
// Count of pairs for index i
// till its set bit position
for (int j = 0;
j <= bitpos; j++)
{
count += set_bits[j];
}
// Increasing count of set bit
// position of current elelement
set_bits[bitpos]++;
}
// Print the answer
Console.Write(count);
}
// Driver Code
static public void Main()
{
int[] arr = { 4, 16, 8, 64 };
int N = arr.Length;
// Function Call
numberOfPairs(arr, N);
}
}
// This code is contributed by splevel62.
Javascript
5
时间复杂度: O(N*log M),其中 M 是数组中最大的元素。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live