Number of pairs whose product is a power of 2

Given an array arr[] consisting of N integers, the task is to count the total number of pairs of array elements from the given array such that arr[i] * arr[j] is the power of 2.

Examples:

Input: arr[] = {2, 4, 7, 2}
Output: 3
Explanation:
arr[0] * arr[1] = 8
arr[0] * arr[3] = 4
arr[1] * arr[3] = 8

Input: arr[] = {8, 1, 12, 4, 2}
Output: 6

 

Approach: The idea is based upon the fact that a number is a power of 2 if it contains only 2 as its prime factor. Therefore, all its divisors are also a power of 2. Follow the steps below to solve the problem:



  1. Traverse the given array.
  2. For every array element, check if it is a power of 2 or not. Increase the count of such elements
  3. Finally, print (count * (count – 1)) / 2 as the required count.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output: 
3











 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :