GeeksforGeeks App
Open App
Browser
Continue

# Find all pairs raised to power K differs by exactly N

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. (1, -2): The value of (15 – (-2)5) = 33, which is equal to X(= 33).
2. (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 ``using` `namespace` `std;` `// Function to print pairs whose``// difference raised to the power K is X``void` `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 Code``int` `main()``{``    ``long` `long` `int` `X = 33;``    ``int` `K = 5;``    ``ValidPairs(X, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to print pairs whose``// difference raised to the power K is X``static` `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 Code``public` `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 print pairs whose``# difference raised to the power K is X``def` `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 Code``X ``=` `33``K ``=` `5``ValidPairs(X, K)` `# This code is contributed by sanjoy_62.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to print pairs whose``// difference raised to the power K is X``static` `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 Code``static` `public` `void` `Main()``{``    ``int` `X = 33;``    ``int` `K = 5;` `    ``ValidPairs(X, K);``}``}` `// This code is contributed by Dharanendra L V.`

## Javascript

 ``

Output:

```1 -2
2 -1```

Time Complexity: O(2000 * 2000)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up