PHP Program for Pairs such that one is a power multiple of other
Last Updated :
23 Jul, 2024
You are given an array A[] of n-elements and a positive integer k (k > 1). 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-1; i++)
{
for (int j=i+1; j ? n-1; 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;
Below is the implementation of above approach:
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.
?>
Complexity Analysis:
- Time Complexity: O(n*n), as nested loops are used
- Auxiliary Space: O(1), as no extra space is used
Please refer complete article on Pairs such that one is a power multiple of other for more details!
Similar Reads
Program to print multiplication table of any number in PHP In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table. We use HTML and PHP to display the multiplication table. The HTML
1 min read
How to Find Multiplication of Two Matrices of any Size in PHP? Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product. Below are the approaches to find the Multiplication of two Matrices of any size in PHP:Table of Content
4 min read
PHP program to print an arithmetic progression series using inbuilt functions We have to print an arithmetic progressive series in PHP, between two given numbers a and b both including, a given common arithmetic difference of d. Examples: Input : $a = 200, $b = 250, $d = 10 Output : 200, 210, 220, 230, 240, 250 Input : $a = 10, $b = 100, $d = 20 Output : 10, 30, 50, 70, 90Th
2 min read
PHP program to swap two numbers Integer values can be stored in a variable in PHP. It is easy to store and modify and swap these integer values using various mentioned approaches: Using a temporary variable: The value of the first number is stored in the temporary variable. This value is then replaced by the value of the second nu
4 min read
PHP gmp_perfect_power() Function The gmp_perfect_power() function is an inbuilt function in PHP that is used to check the perfect power of the number. Syntax: gmp_perfect_power(GMP|int|string $num): boolParameters: This function accepts only one parameter which is described below. $num: A GMP number resource representing the number
2 min read