📜  乘积为 2 的幂的对数

📅  最后修改于: 2021-10-26 06:55:19             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是计算给定数组中数组元素对的总数,使得arr[i] * arr[j]是 2 的幂。

例子:

方法:这个想法是基于这样一个事实:如果一个数只包含2作为它的质因数,那么它就是 2 的幂。因此,它的所有除数也是2。请按照以下步骤解决问题:

  1. 遍历给定数组。
  2. 对于每个数组元素,检查它是否是 2 的幂。增加此类元素的数量
  3. 最后,打印(count * (count – 1)) / 2作为所需的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count pairs having
// product equal to a power of 2
int countPairs(int arr[], int N)
{
    // Stores count of array elements
    // which are power of 2
    int countPowerof2 = 0;
 
    for (int i = 0; i < N; i++) {
 
        // If array element contains
        // only one set bit
        if (__builtin_popcount(arr[i]) == 1)
 
            // Increase count of
            // powers of 2
            countPowerof2++;
    }
 
    // Count required number of pairs
    int desiredPairs
        = (countPowerof2
           * (countPowerof2 - 1))
          / 2;
 
    // Print the required number of pairs
    cout << desiredPairs << ' ';
}
 
// Driver Code
int main()
{
    // Given array
    int arr[4] = { 2, 4, 7, 2 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    countPairs(arr, N);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to count pairs having
// product equal to a power of 2
static void countPairs(int arr[],
                       int N)
{
  // Stores count of array elements
  // which are power of 2
  int countPowerof2 = 0;
 
  for (int i = 0; i < N; i++)
  {
    // If array element contains
    // only one set bit
    if (Integer.bitCount(arr[i]) == 1)
 
      // Increase count of
      // powers of 2
      countPowerof2++;
  }
 
  // Count required number of pairs
  int desiredPairs = (countPowerof2 *
                     (countPowerof2 - 1)) / 2;
 
  // Print the required number of pairs
  System.out.print(desiredPairs + " ");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array
  int arr[] = {2, 4, 7, 2};
 
  // Size of the array
  int N = arr.length;
 
  // Function Call
  countPairs(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function to count pairs having
# product equal to a power of 2
def countPairs(arr, N):
     
    # Stores count of array elements
    # which are power of 2
    countPowerof2 = 0
 
    for i in range(N):
 
        # If array element contains
        # only one set bit
        if (bin(arr[i]).count('1') == 1):
 
            # Increase count of
            # powers of 2
            countPowerof2 += 1
 
    # Count required number of pairs
    desiredPairs = (countPowerof2 *
                   (countPowerof2 - 1)) // 2
 
    # Print the required number of pairs
    print(desiredPairs)
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 2, 4, 7, 2 ]
 
    # Size of the array
    N = len(arr)
 
    # Function call
    countPairs(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
using System.Linq;
 
class GFG{
 
// Function to count pairs having
// product equal to a power of 2
static void countPairs(int []arr,
                       int N)
{
     
    // Stores count of array elements
    // which are power of 2
    int countPowerof2 = 0;
     
    for(int i = 0; i < N; i++)
    {
         
        // If array element contains
        // only one set bit
        if ((Convert.ToString(
             arr[i], 2)).Count(
             f => (f == '1')) == 1)
              
            // Increase count of
            // powers of 2
            countPowerof2++;
    }
     
    // Count required number of pairs
    int desiredPairs = (countPowerof2 *
                       (countPowerof2 - 1)) / 2;
     
    // Print the required number of pairs
    Console.WriteLine(desiredPairs + " ");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array
    int []arr = { 2, 4, 7, 2 };
     
    // Size of the array
    int N = arr.Length;
     
    // Function call
    countPairs(arr, N);
}
}
 
// This code is contributed by math_lover


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程