# Pair of integers having difference of their fifth power as X

Given an integer **X**, the task is to find a pair **A** and **B** such that their difference of fifth power is X, i.e., **A ^{5} – B^{5 }= X**. If there is no such pair print “Not Possible”.

Input:X = 33Output:1 -2Explanation:Input:N = 211Output:-2 -3Explanation:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

**Naive Approach:** A simple solution is to use two for loops, one for A and one for B, ranging from -10^{9} to 10^{9}. **Efficient Approach:** The idea is to **narrow down the range of A and B** using mathematical techniques.

Since A^{5} – B^{5} = X => A^{5} = X + B^{5}. For A to be as high as possible, B also has to be as high as possible, as it is evident from the inequality.

Consider A = N and B = N – 1

=> N^{5}– (N – 1)^{5}= X.

By binomial expansion, we know

(p + 1)y

^{p}<= (y + 1)^{p+1}– y^{p+1}<= (p+1)(y+1)^{p}

So we can say that the maximum value of LHS is 4N^{4}.

Hence 4N

^{5}<= X

=> N <= (X/5)^{1/5}.

=> This gives usN ~ 120.

Since A and B can also be negative, we simply extrapolate the range and the final range we get is **[-120, 120].**

Below is the implementation of the above approach:

## C++

`// C++ implementation to find a pair` `// of integers A & B such that` `// difference of fifth power is` `// equal to the given number X` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find a pair` `// of integers A & B such that` `// difference of fifth power is` `// equal to the given number X` `void` `findPair(` `int` `x)` `{` ` ` `int` `lim = 120;` ` ` `// Loop to choose every possible` ` ` `// pair with in the range` ` ` `for` `(` `int` `i = -lim; i <= lim; i++) {` ` ` `for` `(` `int` `j = -lim; j <= lim; j++) {` ` ` `// Check if equation holds` ` ` `if` `(` `pow` `(i, 5) - ` `pow` `(j, 5) == x) {` ` ` `cout << i << ` `' '` `<< j << endl;` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `cout << ` `"-1"` `;` `}` `// Driver Code` `signed` `main()` `{` ` ` `int` `X = 33;` ` ` `// Function Call` ` ` `findPair(X);` ` ` `return` `0;` `}` |

## Java

`// Java implementation to find a` `// pair of integers A & B such` `// that difference of fifth power` `// is equal to the given number X` `class` `GFG{` `// Function to find a pair` `// of integers A & B such that` `// difference of fifth power is` `// equal to the given number X` `static` `void` `findPair(` `int` `x)` `{` ` ` `int` `lim = ` `120` `;` ` ` `// Loop to choose every possible` ` ` `// pair with in the range` ` ` `for` `(` `int` `i = -lim; i <= lim; i++)` ` ` `{` ` ` `for` `(` `int` `j = -lim; j <= lim; j++)` ` ` `{` ` ` ` ` `// Check if equation holds` ` ` `if` `(Math.pow(i, ` `5` `) -` ` ` `Math.pow(j, ` `5` `) == x)` ` ` `{` ` ` `System.out.print(i + ` `" "` `+` ` ` `j + ` `"\n"` `);` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `System.out.print(` `"-1"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `X = ` `33` `;` ` ` `// Function Call` ` ` `findPair(X);` `}` `}` `// This code is contributed by PrinciRaj1992` |

## Python3

`# Python3 implementation to find ` `# a pair of integers A & B such ` `# that difference of fifth power` `# is equal to the given number X` `import` `math` `# Function to find a pair` `# of integers A & B such that` `# difference of fifth power is` `# equal to the given number X` `def` `findPair(x):` ` ` `lim ` `=` `120` ` ` `# Loop to choose every possible` ` ` `# pair with in the range` ` ` `for` `i ` `in` `range` `(` `-` `lim, lim ` `+` `1` `):` ` ` `for` `j ` `in` `range` `(` `-` `lim, lim ` `+` `1` `):` ` ` ` ` `# Check if equation holds` ` ` `if` `(math.` `pow` `(i, ` `5` `) ` `-` ` ` `math.` `pow` `(j, ` `5` `) ` `=` `=` `x):` ` ` `print` `(i, end ` `=` `' '` `)` ` ` `print` `(j, end ` `=` `'\n'` `)` ` ` `return` ` ` ` ` `print` `(` `"-1"` `)` `# Driver Code` `X ` `=` `33` `# Function Call` `findPair(X)` `# This code is contributed by PratikBasu` |

## C#

`// C# implementation to find a` `// pair of integers A & B such` `// that difference of fifth power` `// is equal to the given number X` `using` `System;` `class` `GFG{` `// Function to find a pair of` `// integers A & B such that` `// difference of fifth power is` `// equal to the given number X` `static` `void` `findPair(` `int` `x)` `{` ` ` `int` `lim = 120;` ` ` `// Loop to choose every possible` ` ` `// pair with in the range` ` ` `for` `(` `int` `i = -lim; i <= lim; i++)` ` ` `{` ` ` `for` `(` `int` `j = -lim; j <= lim; j++)` ` ` `{` ` ` ` ` `// Check if equation holds` ` ` `if` `(Math.Pow(i, 5) -` ` ` `Math.Pow(j, 5) == x)` ` ` `{` ` ` `Console.Write(i + ` `" "` `+` ` ` `j + ` `"\n"` `);` ` ` `return` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `Console.Write(` `"-1"` `);` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `X = 33;` ` ` `// Function call` ` ` `findPair(X);` `}` `}` `// This code is contributed by 29AjayKumar` |

## Javascript

`<script>` `// JavaScript implementation to find a` `// pair of integers A & B such` `// that difference of fifth power` `// is equal to the given number X` `// Function to find a pair` `// of integers A & B such that` `// difference of fifth power is` `// equal to the given number X` `function` `findPair(x)` `{` ` ` `let lim = 120;` ` ` `// Loop to choose every possible` ` ` `// pair with in the range` ` ` `for` `(let i = -lim; i <= lim; i++)` ` ` ` ` `for` `(let j = -lim; j <= lim; j++)` ` ` ` ` ` ` `// Check if equation holds` ` ` `if` `(Math.pow(i, 5) -Math.pow(j, 5) == x)` ` ` `{` ` ` `document.write(i + ` `" "` `+ j);` ` ` `return` `;` ` ` `}` ` ` ` ` `document.write(` `"-1"` `);` `}` `// Driver Code` ` ` `let X = 33;` ` ` `// Function Call` ` ` `findPair(X);` `// This code is contributed by mohan` `</script>` |

**Output:**

1 -2

**Time Complexity:** O(240*240)