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)

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.

## Recommended Posts:

- Highest power of a number that divides other number
- Highest power of 2 that divides a number represented in binary
- Highest power of 2 that divides the LCM of first N Natural numbers.
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find two co-prime integers such that the first divides A and the second divides B
- Count subarrays where second highest lie before highest
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count pairs from an array having product of their sum and difference equal to 0
- Count pairs from an array having product of their sum and difference equal to 1
- Highest power of 2 less than or equal to given number
- Highest power of 2 less than or equal to given Integer
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count pairs in Array whose product is a Kth power of any positive integer
- Find maximum power of a number that divides a factorial
- Count the pairs in an array such that the difference between them and their indices is equal
- Count pairs of numbers from 1 to N with Product divisible by their Sum

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.