给定N个正整数的数组arr [] 。任务是找到对数(arr [i],arr [j]) ,以使将arr [i] * arr [j]除以2的最大幂为1 。
例子:
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
方法:由于除以arr [i] * arr [j]的2的最大幂为max 1 ,这意味着如果P是乘积,则它必须是奇数或2是P的唯一偶数因子。
这意味着arr [i]和arr [j]都必须是奇数,或者恰好其中一个是偶数,而2是该数字的唯一偶数因子。
如果奇数是奇数,偶数是奇数,使得2是该数字的唯一偶数,则答案将是奇*偶+奇*(奇– 1)/ 2 。
下面是上述方法的实现:
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[0]);
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
输出:
12
时间复杂度: O(N)