Find two numbers whose difference of fourth power is equal to N

Given an integer N, the task is to find two non-negative integers X and Y such that X4 – Y4 = N. If no such pair exists, print -1.

Examples:

Input: N = 15
Output: X = 2, Y = 1
Explanation:
X4 – Y4 = (2)4 – (1)4 = (16) – (1) = 15

Input: N = 10
Output: -1
Explanation :
No such value of X and Y are there which satisfy the condition.

Approach:
To solve the problem mentioned above, we have to observe that we need to find the minimum and the maximum values of x and y that is possible to satisfy the equation.



  • The minimum value for the two integers can be 0 since X & Y are non-negative.
  • The maximum value of X and Y can be ceil(N(1/4)).
  • Hence, iterate over the range [0, ceil(N(1/4))] and find any suitable pair of X and Y that satisfies the condition.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// values of x and y for the given
// equation with integer N
  
#include <bits/stdc++.h>
using namespace std;
  
// Function which find required x & y
void solve(int n)
{
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = ceil(pow(
        n, 1.0 / 4));
  
    for (int x = 0; x <= upper_limit; x++) {
  
        for (int y = 0; y <= upper_limit; y++) {
  
            // num1 stores x^4
            int num1 = x * x * x * x;
  
            // num2 stores y^4
            int num2 = y * y * y * y;
  
            // If condition is satisfied
            // the print and return
            if (num1 - num2 == n) {
                cout << "x = " << x
                     << ", y = " << y;
                return;
            }
        }
    }
  
    // If no such pair exists
    cout << -1 << endl;
}
  
// Driver code
int main()
{
    int n = 15;
  
    solve(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the
// values of x and y for the given
// equation with integer N
import java.util.*;
  
class GFG{
  
// Function which find required x & y
static void solve(int n)
{
      
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = (int) (Math.ceil
                            (Math.pow(n, 1.0 / 4)));
  
    for(int x = 0; x <= upper_limit; x++)
    {
       for(int y = 0; y <= upper_limit; y++) 
       {
            
          // num1 stores x^4
          int num1 = x * x * x * x;
            
          // num2 stores y^4
          int num2 = y * y * y * y;
            
          // If condition is satisfied
          // the print and return
          if (num1 - num2 == n) 
          {
              System.out.print("x = " + x +
                             ", y = " + y);
              return;
          }
       }
    }
      
    // If no such pair exists
    System.out.print(-1);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 15;
  
    solve(n);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find the
# values of x and y for the given
# equation with integer N
from math import pow, ceil
  
# Function which find required x & y
def solve(n) :
  
    # Upper limit of x & y,
    # if such x & y exists
    upper_limit = ceil(pow(n, 1.0 / 4));
  
    for x in range(upper_limit + 1) :
  
        for y in range(upper_limit + 1) :
  
            # num1 stores x^4
            num1 = x * x * x * x;
  
            # num2 stores y^4
            num2 = y * y * y * y;
  
            # If condition is satisfied
            # the print and return
            if (num1 - num2 == n) :
                print("x =", x, ", y =" , y);
                return;
  
    # If no such pair exists
    print(-1) ;
  
# Driver code
if __name__ == "__main__" :
  
    n = 15;
  
    solve(n);
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the
// values of x and y for the given
// equation with integer N
using System;
  
class GFG{
  
// Function which find required x & y
static void solve(int n)
{
      
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = (int) (Math.Ceiling
                            (Math.Pow(n, 1.0 / 4)));
  
    for(int x = 0; x <= upper_limit; x++)
    {
       for(int y = 0; y <= upper_limit; y++) 
       {
            
          // num1 stores x^4
          int num1 = x * x * x * x;
            
          // num2 stores y^4
          int num2 = y * y * y * y;
            
          // If condition is satisfied
          // the print and return
          if (num1 - num2 == n) 
          {
              Console.Write("x = " + x +
                          ", y = " + y);
              return;
          }
       }
    }
      
    // If no such pair exists
    Console.Write(-1);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 15;
  
    solve(n);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Output:

x = 2, y = 1

Time Complexity: O(sqrt(N))

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.