Count of pairs in an array such that the highest power of 2 that divides their product is 1
Last Updated :
28 May, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int cntPairs( int a[], int n)
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 1)
odd++;
else if ((a[i] / 2) % 2 == 1)
even++;
}
int ans = odd * even + (odd * (odd - 1)) / 2;
return ans;
}
int main()
{
int a[] = { 4, 2, 7, 11, 14, 15, 18 };
int n = sizeof (a) / sizeof (a[0]);
cout << cntPairs(a, n);
return 0;
}
|
Java
class GFG
{
static int cntPairs( int a[], int n)
{
int odd = 0 , even = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] % 2 == 1 )
odd++;
else if ((a[i] / 2 ) % 2 == 1 )
even++;
}
int ans = odd * even + (odd * (odd - 1 )) / 2 ;
return ans;
}
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));
}
}
|
Python3
def cntPairs(a, n) :
odd = 0 ; even = 0 ;
for i in range (n) :
if (a[i] % 2 = = 1 ) :
odd + = 1 ;
elif ((a[i] / 2 ) % 2 = = 1 ) :
even + = 1 ;
ans = odd * even + (odd * (odd - 1 )) / / 2 ;
return ans;
if __name__ = = "__main__" :
a = [ 4 , 2 , 7 , 11 , 14 , 15 , 18 ];
n = len (a);
print (cntPairs(a, n));
|
C#
using System;
class GFG
{
static int cntPairs( int []a, int n)
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
odd++;
else if ((a[i] / 2) % 2 == 1)
even++;
}
int ans = odd * even + (odd * (odd - 1)) / 2;
return ans;
}
public static void Main(String []args)
{
int []a = { 4, 2, 7, 11, 14, 15, 18 };
int n = a.Length;
Console.WriteLine(cntPairs(a, n));
}
}
|
Javascript
<script>
function cntPairs(a, n)
{
var odd = 0, even = 0;
for ( var i = 0; i < n; i++) {
if (a[i] % 2 == 1)
odd++;
else if ((a[i] / 2) % 2 == 1)
even++;
}
var ans = odd * even + (odd * (odd - 1)) / 2;
return ans;
}
var a = [4, 2, 7, 11, 14, 15, 18];
var n = a.length;
document.write( cntPairs(a, n));
</script>
|
Time Complexity: O(n) where n is number of elements in given array. As, we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...