给定数组arr [] ,任务是对数组中的对进行计数,以使每个对中的一个元素成为另一对元素的幂。
例子:
Input: arr[] = {16, 2, 3, 9}
Output: 2
The 2 pairs are (16, 2) and (3, 9)
Input: arr[] = {2, 3, 5, 7}
Output: 0
方法:
- 将数组作为输入后,首先我们需要找出该数组中所有可能的对。
- 因此,从数组中找出对
- 然后,对于每一对,检查一个元素是否是另一个的幂。如果是这样,则将所需的计数增加一。
- 检查所有对后,返回或打印该对的计数。
下面是上述方法的实现:
C++
// C++ program to count pairs in array
// such that one element is power of another
#include
using namespace std;
// Function to check if given number number y
// is power of x
bool isPower(int x, int y)
{
// log function to calculate value
int res1 = log(y) / log(x);
double res2 = log(y) / log(x);
// compare to the result1
// or result2 both are equal
return (res1 == res2);
}
// Function to find pairs from array
int countPower(int arr[], int n)
{
int res = 0;
// Iterate through all pairs
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
// Increment count if one is
// the power of other
if (isPower(arr[i], arr[j])
|| isPower(arr[j], arr[i]))
res++;
return res;
}
// Driver code
int main()
{
int a[] = { 16, 2, 3, 9 };
int n = sizeof(a) / sizeof(a[0]);
cout << countPower(a, n);
return 0;
}
Java
// Java program to count pairs in array
// such that one element is power of another
class GFG
{
// Function to check if given number number y
// is power of x
static boolean isPower(int x, int y)
{
// log function to calculate value
int res1 = (int)(Math.log(y) / Math.log(x));
double res2 = Math.log(y) / Math.log(x);
// compare to the result1
// or result2 both are equal
return (res1 == res2);
}
// Function to find pairs from array
static int countPower(int arr[], int n)
{
int res = 0;
// Iterate through all pairs
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
// Increment count if one is
// the power of other
if (isPower(arr[i], arr[j])
|| isPower(arr[j], arr[i]))
res++;
return res;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 16, 2, 3, 9 };
int n =a.length;
System.out.println(countPower(a, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to count pairs in array
# such that one element is power of another
from math import log
# Function to check if given number number y
# is power of x
def isPower(x, y) :
# log function to calculate value
res1 = log(y) // log(x);
res2 = log(y) / log(x);
# compare to the result1
# or result2 both are equal
return (res1 == res2);
# Function to find pairs from array
def countPower( arr, n) :
res = 0;
# Iterate through all pairs
for i in range(n) :
for j in range(i + 1, n) :
# Increment count if one is
# the power of other
if isPower(arr[i], arr[j]) or isPower(arr[j], arr[i]) :
res += 1;
return res;
# Driver code
if __name__ == "__main__" :
a = [ 16, 2, 3, 9 ];
n = len(a);
print(countPower(a, n));
# This code is contributed by AnkitRai01
C#
// C# program to count pairs in array
// such that one element is power of another
using System;
public class GFG
{
// Function to check if given number number y
// is power of x
static bool isPower(int x, int y)
{
// log function to calculate value
int res1 = (int)(Math.Log(y) / Math.Log(x));
double res2 = Math.Log(y) / Math.Log(x);
// compare to the result1
// or result2 both are equal
return (res1 == res2);
}
// Function to find pairs from array
static int countPower(int []arr, int n)
{
int res = 0;
// Iterate through all pairs
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
// Increment count if one is
// the power of other
if (isPower(arr[i], arr[j])
|| isPower(arr[j], arr[i]))
res++;
return res;
}
// Driver code
public static void Main ()
{
int []a = { 16, 2, 3, 9 };
int n =a.Length;
Console.WriteLine(countPower(a, n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
2