You are given an array A[] of n-elements and a positive integer k. Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer.
Note: (Ai, Aj) and (Aj, Ai) must be count once.
Examples :
Input : A[] = {3, 6, 4, 2}, k = 2
Output : 2
Explanation : We have only two pairs
(4, 2) and (3, 6)
Input : A[] = {2, 2, 2}, k = 2
Output : 3
Explanation : (2, 2), (2, 2), (2, 2)
that are (A1, A2), (A2, A3) and (A1, A3) are
total three pairs where Ai = Aj * (k^0)
To solve this problem, we first sort the given array and then for each element Ai, we find number of elements equal to value Ai * k^x for different value of x till Ai * k^x is less than or equal to largest of Ai.
Algorithm:
// sort the given array
sort(A, A+n);
// for each A[i] traverse rest array
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
// count Aj such that Ai*k^x = Aj
int x = 0;
// increase x till Ai * k^x <=
// largest element
while ((A[i]*pow(k, x)) <= A[j])
{
if ((A[i]*pow(k, x)) == A[j])
{
ans++;
break;
}
x++;
}
}
}
// return answer
return ans;
C++
// Program to find pairs count #include <bits/stdc++.h> using namespace std; // function to count the required pairs int countPairs(int A[], int n, int k) { int ans = 0; // sort the given array sort(A, A + n); // for each A[i] traverse rest array for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // count Aj such that Ai*k^x = Aj int x = 0; // increase x till Ai * k^x <= largest element while ((A[i] * pow(k, x)) <= A[j]) { if ((A[i] * pow(k, x)) == A[j]) { ans++; break; } x++; } } } return ans; } // driver program int main() { int A[] = {3, 8, 9, 12, 18, 4, 24, 2, 6}; int n = sizeof(A) / sizeof(A[0]); int k = 3; cout << countPairs(A, n, k); return 0; } |
Java
// Java program to find pairs count import java.io.*; import java .util.*; class GFG { // function to count the required pairs static int countPairs(int A[], int n, int k) { int ans = 0; // sort the given array Arrays.sort(A); // for each A[i] traverse rest array for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // count Aj such that Ai*k^x = Aj int x = 0; // increase x till Ai * k^x <= largest element while ((A[i] * Math.pow(k, x)) <= A[j]) { if ((A[i] * Math.pow(k, x)) == A[j]) { ans++; break; } x++; } } } return ans; } // Driver program public static void main (String[] args) { int A[] = {3, 8, 9, 12, 18, 4, 24, 2, 6}; int n = A.length; int k = 3; System.out.println (countPairs(A, n, k)); } } // This code is contributed by vt_m. |
Python3
# Program to find pairs count import math # function to count the required pairs def countPairs(A, n, k): ans = 0 # sort the given array A.sort() # for each A[i] traverse rest array for i in range(0,n): for j in range(i + 1, n): # count Aj such that Ai*k^x = Aj x = 0 # increase x till Ai * k^x <= largest element while ((A[i] * math.pow(k, x)) <= A[j]) : if ((A[i] * math.pow(k, x)) == A[j]) : ans+=1 break x+=1 return ans # driver program A = [3, 8, 9, 12, 18, 4, 24, 2, 6] n = len(A) k = 3 print(countPairs(A, n, k)) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to find pairs count using System; class GFG { // function to count the required pairs static int countPairs(int []A, int n, int k) { int ans = 0; // sort the given array Array.Sort(A); // for each A[i] traverse rest array for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // count Aj such that Ai*k^x = Aj int x = 0; // increase x till Ai * k^x <= largest element while ((A[i] * Math.Pow(k, x)) <= A[j]) { if ((A[i] * Math.Pow(k, x)) == A[j]) { ans++; break; } x++; } } } return ans; } // Driver program public static void Main () { int []A = {3, 8, 9, 12, 18, 4, 24, 2, 6}; int n = A.Length; int k = 3; Console.WriteLine(countPairs(A, n, k)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to find pairs count // function to count // the required pairs function countPairs($A, $n, $k) { $ans = 0; // sort the given array sort($A); // for each A[i] // traverse rest array for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { // count Aj such that Ai*k^x = Aj $x = 0; // increase x till Ai * // k^x <= largest element while (($A[$i] * pow($k, $x)) <= $A[$j]) { if (($A[$i] * pow($k, $x)) == $A[$j]) { $ans++; break; } $x++; } } } return $ans; } // Driver Code $A = array(3, 8, 9, 12, 18, 4, 24, 2, 6); $n = count($A); $k = 3; echo countPairs($A, $n, $k); // This code is contributed by anuj_67. ?> |
6
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.
Recommended Posts:
- Number of pairs whose sum is a power of 2
- Count of pairs whose bitwise AND is a power of 2
- Count of even and odd power pairs in an Array
- Sum of absolute difference of all pairs raised to power K
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count pairs in Array whose product is a Kth power of any positive integer
- Find the maximum cost of an array of pairs choosing at most K pairs
- Check if bitwise AND of any subset is power of two
- Find number of subarrays with XOR value a power of 2
- Count of elements which cannot form any pair whose sum is power of 2
- Count of distinct power of prime factor of N
- Count of numbers which can be made power of 2 by given operation
- Find the largest possible k-multiple set
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Count of elements which are power of 2 in a given range subarray for Q queries
- Elements of Array which can be expressed as power of prime numbers
- Minimum 1s to lend power to make whole array powerful
- Elements of Array which can be expressed as power of some integer to given exponent K
- Structure Sorting (By Multiple Rules) in C++
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 : vt_m

