# Count pairs in array such that one element is power of another

Given an array arr[], the task is to count the pairs in the array such that one element is the power of another in each pair.

Examples:

```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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• After taking the array as input, first we need to find out all the possible pairs in that array.
• So, find out the pairs from the array
• Then for each pair, check whether an element is a power of another. If it is, then increase the required count by one.
• When all the pairs have been checked, return or print the count of such pair.

Below is the implementation of the above approach:

## 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); ` `    ``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 `

Output:

```2
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : AnkitRai01

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.