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

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

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

12

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count pairs in Array whose product is a Kth power of any positive integer
- Highest power of 2 that divides a number represented in binary
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Highest power of a number that divides other number
- Count of even and odd power pairs in an Array
- Count pairs in array such that one element is power of another
- Count pairs in a sorted array whose product is less than k
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of pairs in an array whose product is a perfect square
- Count of index pairs in array whose range product is a positive integer
- Find an element which divides the array in two subarrays with equal product
- Count of pairs whose bitwise AND is a power of 2
- Count subarrays where second highest lie before highest
- Count ordered pairs with product less than N
- Highest power of 2 less than or equal to given Integer
- Highest power of 2 less than or equal to given number
- Count number of ordered pairs with Even and Odd Product
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count all distinct pairs with product equal to K
- Highest and Smallest power of K less than and greater than equal to N respectively

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.