给定N个元素的数组。任务是对数组中的无序对(i,j)进行计数,以便a [i]和a [j]的乘积可以表示为2的幂。
例子:
Input : arr[] = {2, 3, 4, 8, 10}
Output : 3
Explanation: The pair of array element will be
(2, 4), (2, 8), (4, 8) whose product are
8, 16, 32 respectively which can be expressed
as power of 2, like 2^3, 2^4, 2^5.
Input : arr[] = { 2, 5, 8, 16, 128 }
Output : 6
如果乘和他们的产品成为 ,则z = x * y,如果可以表达的话作为两个的幂,那么可以证明和可以表示为2的幂。基本上z = 2 a = 2 (b + c) = 2 b * 2 c = x * y 和两个都
可以保持最小值0。
因此,现在我们必须计算数组中可以表示为2的幂的元素的数量。如果计数为k,则答案将为kC2 = k *(k-1)/ 2,因为我们需要无序对的计数。
下面是上述方法的实现:
C++
// C++ program to Count unordered pairs (i, j)
// in array such that product of a[i] and a[j]
// can be expressed as power of two
#include
using namespace std;
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
// Function to Count unordered pairs
void Count_pairs(int a[], int n)
{
int count = 0;
for (int i = 0; i < n; i++) {
// is a number can be expressed
// as power of two
if (isPowerOfTwo(a[i]))
count++;
}
// count total number
// of unordered pairs
int ans = (count * (count - 1)) / 2;
cout << ans << "\n";
}
// Driver code
int main()
{
int a[] = { 2, 5, 8, 16, 128 };
int n = sizeof(a) / sizeof(a[0]);
Count_pairs(a, n);
return 0;
}
Java
// Java program to Count unordered pairs (i, j)
// in array such that product of a[i] and a[j]
// can be expressed as power of two
import java.io.*;
class GFG {
/* Function to check if x is power of 2*/
static boolean isPowerOfTwo(int x)
{
/* First x in the below expression is
for the case when x is 0 */
return (x >0&& (!((x&(x-1))>0)));
}
// Function to Count unordered pairs
static void Count_pairs(int a[], int n)
{
int count = 0;
for (int i = 0; i < n; i++) {
// is a number can be expressed
// as power of two
if (isPowerOfTwo(a[i]))
count++;
}
// count total number
// of unordered pairs
int ans = (count * (count - 1)) / 2;
System.out.println( ans);
}
// Driver code
public static void main (String[] args) {
int a[] = { 2, 5, 8, 16, 128 };
int n = a.length;
Count_pairs(a, n);
}
}
// This code is contributed
// by shs
Python 3
# Python3 program to Count unordered pairs
# (i, j) in array such that product of a[i]
# and a[j] can be expressed as power of two
# Function to check if x is power of 2
def isPowerOfTwo(x) :
# First x in the below expression
# is for the case when x is 0
return (x and(not(x & (x - 1))))
# Function to Count unordered pairs
def Count_pairs(a, n) :
count = 0
for i in range(n) :
# is a number can be expressed
# as power of two
if isPowerOfTwo(a[i]) :
count += 1
# count total number
# of unordered pairs
ans = (count * (count - 1)) / 2
print(ans)
# Driver code
if __name__ == "__main__" :
a = [ 2, 5, 8, 16, 128]
n = len(a)
Count_pairs(a, n)
# This code is contributed by ANKITRAI1
C#
// C# program to Count unordered pairs (i, j)
// in array such that product of a[i] and a[j]
// can be expressed as power of two
using System;
public class GFG{
/* Function to check if x is power of 2*/
static bool isPowerOfTwo(int x)
{
/* First x in the below expression is
for the case when x is 0 */
return (x >0&& (!((x&(x-1))>0)));
}
// Function to Count unordered pairs
static void Count_pairs(int []a, int n)
{
int count = 0;
for (int i = 0; i < n; i++) {
// is a number can be expressed
// as power of two
if (isPowerOfTwo(a[i]))
count++;
}
// count total number
// of unordered pairs
int ans = (count * (count - 1)) / 2;
Console.WriteLine( ans);
}
// Driver code
static public void Main (){
int []a = { 2, 5, 8, 16, 128 };
int n = a.Length;
Count_pairs(a, n);
}
}
// This code is contributed
// by Sach_Code
PHP
Javascript
输出:
6
时间复杂度: O(N),其中N是数组中元素的数量。