Related Articles

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

• Last Updated : 25 May, 2021

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, 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++

 `// C++ implementation of the approach``#include ``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);` `    ``cout << cntPairs(a, n);` `    ``return` `0;``}`

## Java

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

## Python3

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

## C#

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

## Javascript

 ``
Output:

`12`

Time Complexity: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up