Count of pairs in an array such that the highest power of 2 that divides their product is 1

Given an array arr[] of N positive integers. The task is to find the count of pairs (arr[i], arr[j]) such that the maximum power of 2 that divides arr[i] * arr[j] is 1.

Examples:

Input: arr[] = {3, 5, 2, 8}
Output: 3
(3, 2), (5, 2) and (3, 5) are the only valid pairs.
Since the power of 2 that divides 3 * 2 = 6 is 1,
5 * 2 = 10 is 1 and 3 * 5 = 15 is 0.



Input: arr[] = {4, 2, 7, 11, 14, 15, 18}
Output: 12

Approach: As the maximum power of 2 that divides arr[i] * arr[j] is at max 1, it means that if P is the product then it must either be odd or 2 is the only even factor of P.
It implies that both arr[i] and arr[j] must be odd or exactly one of them is even and 2 is the only even factor of this number.
If odd is the count of odd numbers and even is the count of even numbers such that 2 is the only even factor of that number then the answer will be odd * even + odd * (odd – 1) / 2.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count of valid pairs
int cntPairs(int a[], int n)
{
  
    // To store the count of odd numbers and
    // the count of even numbers such that 2
    // is the only even factor of that number
    int odd = 0, even = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If current number is odd
        if (a[i] % 2 == 1)
            odd++;
  
        // If current number is even and 2
        // is the only even factor of it
        else if ((a[i] / 2) % 2 == 1)
            even++;
    }
  
    // Calculate total number of valid pairs
    int ans = odd * even + (odd * (odd - 1)) / 2;
  
    return ans;
}
  
// Driver code
int main()
{
  
    int a[] = { 4, 2, 7, 11, 14, 15, 18 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << cntPairs(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
// Function to return the count of valid pairs
static int cntPairs(int a[], int n)
{
  
    // To store the count of odd numbers and
    // the count of even numbers such that 2
    // is the only even factor of that number
    int odd = 0, even = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // If current number is odd
        if (a[i] % 2 == 1)
            odd++;
  
        // If current number is even and 2
        // is the only even factor of it
        else if ((a[i] / 2) % 2 == 1)
            even++;
    }
  
    // Calculate total number of valid pairs
    int ans = odd * even + (odd * (odd - 1)) / 2;
  
    return ans;
}
  
// Driver code
public static void main(String []args) 
{
    int a[] = { 4, 2, 7, 11, 14, 15, 18 };
    int n = a.length;
  
    System.out.println(cntPairs(a, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the count of valid pairs 
def cntPairs(a, n) : 
  
    # To store the count of odd numbers and 
    # the count of even numbers such that 2 
    # is the only even factor of that number 
    odd = 0; even = 0
  
    for i in range(n) :
  
        # If current number is odd 
        if (a[i] % 2 == 1) :
            odd += 1
  
        # If current number is even and 2 
        # is the only even factor of it 
        elif ((a[i] / 2) % 2 == 1) :
            even += 1
      
    # Calculate total number of valid pairs 
    ans = odd * even + (odd * (odd - 1)) // 2
  
    return ans; 
  
# Driver code 
if __name__ == "__main__" :
  
    a = [ 4, 2, 7, 11, 14, 15, 18 ]; 
    n = len(a); 
  
    print(cntPairs(a, n)); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the count of valid pairs
static int cntPairs(int []a, int n)
{
  
    // To store the count of odd numbers and
    // the count of even numbers such that 2
    // is the only even factor of that number
    int odd = 0, even = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // If current number is odd
        if (a[i] % 2 == 1)
            odd++;
  
        // If current number is even and 2
        // is the only even factor of it
        else if ((a[i] / 2) % 2 == 1)
            even++;
    }
  
    // Calculate total number of valid pairs
    int ans = odd * even + (odd * (odd - 1)) / 2;
  
    return ans;
}
  
// Driver code
public static void Main(String []args) 
{
    int []a = { 4, 2, 7, 11, 14, 15, 18 };
    int n = a.Length;
  
    Console.WriteLine(cntPairs(a, n));
}
}
  
// This code is contributed by Ajay KUmar

chevron_right


Output:

12

Time Complexity: O(N)



My Personal Notes arrow_drop_up

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.



Improved By : AnkitRai01, 29AjayKumar