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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to print pairs whose// difference raised to the power K is Xvoid ValidPairs(int X, int K){ // Stores the count of valid pairs long long int count = 0; // Iterate over the range [-1000, 1000] for (int A = -1000; A <= 1000; A++) { // Iterate over the range [-1000, 1000] for (int B = -1000; B <= 1000; B++) { // If the current pair satisfies // the given condition if (pow(A, K) - pow(B, K) == X) { // Increment the count by 1 count++; cout << A << " " << B << endl; } } } // If no such pair exists if (count == 0) { cout << "-1"; }}// Driver Codeint main(){ long long int X = 33; int K = 5; ValidPairs(X, K); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to print pairs whose// difference raised to the power K is Xstatic void ValidPairs(int X, int K){ // Stores the count of valid pairs int count = 0; // Iterate over the range [-1000, 1000] for(int A = -1000; A <= 1000; A++) { // Iterate over the range [-1000, 1000] for(int B = -1000; B <= 1000; B++) { // If the current pair satisfies // the given condition if (Math.pow(A, K) - Math.pow(B, K) == X) { // Increment the count by 1 count++; System.out.println(A + " " + B ); } } } // If no such pair exists if (count == 0) { System.out.println("-1"); }}// Driver Codepublic static void main(String args[]){ int X = 33; int K = 5; ValidPairs(X, K);}}// This code is contributed by souravghosh0416 |
Python3
# Python program for the above approach# Function to prpairs whose# difference raised to the power K is Xdef ValidPairs(X, K) : # Stores the count of valid pairs count = 0 # Iterate over the range [-1000, 1000] for A in range(-1000, 1001, 1): # Iterate over the range [-1000, 1000] for B in range(-1000, 1001, 1): # If the current pair satisfies # the given condition if (pow(A, K) - pow(B, K) == X) : # Increment the count by 1 count += 1 print(A, B) # If no such pair exists if (count == 0) : cout << "-1" # Driver CodeX = 33K = 5ValidPairs(X, K)# This code is contributed by sanjoy_62. |
C#
// C# program for the above approachusing System;class GFG{// Function to print pairs whose// difference raised to the power K is Xstatic void ValidPairs(int X, int K){ // Stores the count of valid pairs int count = 0; // Iterate over the range [-1000, 1000] for(int A = -1000; A <= 1000; A++) { // Iterate over the range [-1000, 1000] for(int B = -1000; B <= 1000; B++) { // If the current pair satisfies // the given condition if (Math.Pow(A, K) - Math.Pow(B, K) == X) { // Increment the count by 1 count++; Console.WriteLine(A + " " + B); } } } // If no such pair exists if (count == 0) { Console.WriteLine("-1"); }}// Driver Codestatic public void Main(){ int X = 33; int K = 5; ValidPairs(X, K);}}// This code is contributed by Dharanendra L V. |
Javascript
<script>// javascript program for the above approach // Function to prvar pairs whose // difference raised to the power K is X function ValidPairs(X , K) { // Stores the count of valid pairs var count = 0; // Iterate over the range [-1000, 1000] for (A = -1000; A <= 1000; A++) { // Iterate over the range [-1000, 1000] for (B = -1000; B <= 1000; B++) { // If the current pair satisfies // the given condition if (Math.pow(A, K) - Math.pow(B, K) == X) { // Increment the count by 1 count++; document.write(A + " " + B +"<br/>"); } } } // If no such pair exists if (count == 0) { document.write("-1<br/>"); } } // Driver Code var X = 33; var K = 5; ValidPairs(X, K);// This code is contributed by gauravrajput1</script> |
1 -2 2 -1
Time Complexity: O(2000 * 2000)
Auxiliary Space: O(1)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


