📌  相关文章
📜  计数对由一个元素组成,该元素对可以从2的幂组成的数组中被另一个元素整除

📅  最后修改于: 2021-05-25 07:05:40             🧑  作者: Mango

给定由2N次方组成的数组arr [] ,任务是计算对数(arr [i],arr [j]) ,以使i arr [j]arr [i]整除。 。

例子:

天真的方法:最简单的方法是生成给定数组arr []的所有对,并针对每一对检查arr [j]%arr [i]是否为0 。如果确定为true,则将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)