Find all pairs raised to power K differs by exactly N
Last Updated :
20 Jan, 2022
Given two positive integers X and K, the task is to find all possible pair of integers (A, B) such that the difference between the pair of integers raised to the power K is the given integer X. If there exists no such pair, then print “-1”.
Note: The value of K is at least 5 and X is at most 1018.
Examples:
Input: X = 33, K = 5
Output:
(1, -2)
(2, -1)
Explanation: All the possible pairs are as follows:
- (1, -2): The value of (15 – (-2)5) = 33, which is equal to X(= 33).
- (2, -1): The value of (25 – (-1)5) = 33, which is equal to X(= 33).
Therefore, the total number of pairs are 2.
Input: X = 10, K = 5
Output: 0
Approach: The given problem can be solved based on the observation that the maximum possible value of X can be 1018. Therefore, the value of the pair of integers (A, B) will lie over the range [-1000, 1000]. Follow the steps below to solve the problem:
- Initialize a variable, say count as 0, to count the number of pairs that satisfies the given conditions.
- Generate all possible pairs (A, B) over the range [-1000, 1000] and if the value of (AK – BK) is X, then print the corresponding pair and increment count by 1.
- After completing the above steps, if the value of count is 0, then print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ValidPairs( int X, int K)
{
long long int count = 0;
for ( int A = -1000; A <= 1000; A++) {
for ( int B = -1000; B <= 1000; B++) {
if ( pow (A, K) - pow (B, K) == X) {
count++;
cout << A << " " << B << endl;
}
}
}
if (count == 0) {
cout << "-1" ;
}
}
int main()
{
long long int X = 33;
int K = 5;
ValidPairs(X, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void ValidPairs( int X, int K)
{
int count = 0 ;
for ( int A = - 1000 ; A <= 1000 ; A++)
{
for ( int B = - 1000 ; B <= 1000 ; B++)
{
if (Math.pow(A, K) - Math.pow(B, K) == X)
{
count++;
System.out.println(A + " " + B );
}
}
}
if (count == 0 )
{
System.out.println( "-1" );
}
}
public static void main(String args[])
{
int X = 33 ;
int K = 5 ;
ValidPairs(X, K);
}
}
|
Python3
def ValidPairs(X, K) :
count = 0
for A in range ( - 1000 , 1001 , 1 ):
for B in range ( - 1000 , 1001 , 1 ):
if ( pow (A, K) - pow (B, K) = = X) :
count + = 1
print (A, B)
if (count = = 0 ) :
cout << "-1"
X = 33
K = 5
ValidPairs(X, K)
|
C#
using System;
class GFG{
static void ValidPairs( int X, int K)
{
int count = 0;
for ( int A = -1000; A <= 1000; A++)
{
for ( int B = -1000; B <= 1000; B++)
{
if (Math.Pow(A, K) - Math.Pow(B, K) == X)
{
count++;
Console.WriteLine(A + " " + B);
}
}
}
if (count == 0)
{
Console.WriteLine( "-1" );
}
}
static public void Main()
{
int X = 33;
int K = 5;
ValidPairs(X, K);
}
}
|
Javascript
<script>
function ValidPairs(X , K) {
var count = 0;
for (A = -1000; A <= 1000; A++) {
for (B = -1000; B <= 1000; B++) {
if (Math.pow(A, K) - Math.pow(B, K) == X) {
count++;
document.write(A + " " + B + "<br/>" );
}
}
}
if (count == 0) {
document.write( "-1<br/>" );
}
}
var X = 33;
var K = 5;
ValidPairs(X, K);
</script>
|
Time Complexity: O(2000 * 2000)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...