Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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++

// 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;
}

                    

Java

// 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

                    

Python3

# 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

                    

C#

// 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

                    

Javascript

<script>
 
    // Javascript implementation to find the
    // values of x and y for the given
    // equation with integer N
     
    // Function which find required x & y
    function solve(n)
    {
        // Upper limit of x & y,
        // if such x & y exists
        let upper_limit = Math.ceil(Math.pow(n, 1.0 / 4));
 
        for (let x = 0; x <= upper_limit; x++) {
 
            for (let y = 0; y <= upper_limit; y++) {
 
                // num1 stores x^4
                let num1 = x * x * x * x;
 
                // num2 stores y^4
                let num2 = y * y * y * y;
 
                // If condition is satisfied
                // the print and return
                if (num1 - num2 == n) {
                    document.write("x = " + x + ", y = " + y);
                    return;
                }
            }
        }
 
        // If no such pair exists
        document.write(-1);
    }
     
    let n = 15;
   
    solve(n);
 
 
</script>

                    

Output
x = 2, y = 1









Time Complexity: O(sqrt(N))
Auxiliary space: O(1)

Another Approach:

In this implementation, we create an unordered map (hash table) to store the values of x^4 – n for each x from 0 to the upper limit. Then, we iterate through the possible values of y and check if y^4 – n is already in the hash table. If it is, we retrieve the corresponding value of x and print the solution. If no such pair exists, we print -1.

C++

#include <bits/stdc++.h>
using namespace std;
 
void solve(int n) {
    unordered_map<int, int> hashTable;
 
    // Calculate upper limit of x & y
    int upper_limit = ceil(pow(n, 1.0/4));
 
    // Store x^4 - n in hash table
    for (int x = 0; x <= upper_limit; x++) {
        int x4 = x * x * x * x;
        hashTable[x4 - n] = x;
    }
 
    // Check if there is a y such that y^4 - x^4 = n
    for (int y = 0; y <= upper_limit; y++) {
        int y4 = y * y * y * y;
        if (hashTable.find(y4) != hashTable.end()) {
            int x = hashTable[y4];
            cout << "x = " << x << ", y = " << y << endl;
            return;
        }
    }
 
    // If no such pair exists
    cout << -1 << endl;
}
 
int main() {
    int n = 15;
 
    solve(n);
 
    return 0;
}

                    

Java

//Java implementation to find the
// values of x and y for the given
// equation with integer N
 
import java.util.HashMap;
 
public class EquationSolver {
 
    // Function which finds required x & y
    static void solve(int n) {
        HashMap<Integer, Integer> hashTable = new HashMap<>();
 
        // Calculate upper limit of x & y
        int upperLimit = (int) Math.ceil(Math.pow(n, 1.0 / 4));
 
        // Store x^4 - n in the hash table
        for (int x = 0; x <= upperLimit; x++) {
            int x4 = (int) Math.pow(x, 4);
            hashTable.put(x4 - n, x);
        }
 
        // Check if there is a y such that y^4 - x^4 = n
        for (int y = 0; y <= upperLimit; y++) {
            int y4 = (int) Math.pow(y, 4);
            if (hashTable.containsKey(y4)) {
                int x = hashTable.get(y4);
                System.out.println("x = " + x + ", y = " + y);
                return;
            }
        }
 
        // If no such pair exists
        System.out.println(-1);
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 15;
        solve(n);
    }
}

                    

Python3

# Python3 implementation to find the
# values of x and y for the given
# equation with integer N
import math
 
# Function which find required x & y
def solve(n):
    hash_table = {}
 
    # Calculate upper limit of x & y
    upper_limit = math.ceil(n ** (1.0/4))
 
    # Store x^4 - n in the hash table
    for x in range(upper_limit + 1):
        x4 = x ** 4
        hash_table[x4 - n] = x
 
    # Check if there is a y such that y^4 - x^4 = n
    for y in range(upper_limit + 1):
        y4 = y ** 4
        if y4 in hash_table:
            x = hash_table[y4]
            print(f"x = {x}, y = {y}")
            return
 
    # If no such pair exists
    print(-1)
#Driver code
def main():
    n = 15
    solve(n)
 
if __name__ == "__main__":
    main()

                    

C#

using System;
using System.Collections.Generic;
 
class Program
{
    static void Solve(int n)
    {
        Dictionary<int, int> hashTable = new Dictionary<int, int>();
 
        // Calculate upper limit of x & y
        int upperLimit = (int)Math.Ceiling(Math.Pow(n, 1.0 / 4));
 
        // Store x^4 - n in hash table
        for (int x = 0; x <= upperLimit; x++)
        {
            int x4 = x * x * x * x;
            hashTable[x4 - n] = x;
        }
 
        // Check if there is a y such that y^4 - x^4 = n
        for (int y = 0; y <= upperLimit; y++)
        {
            int y4 = y * y * y * y;
            if (hashTable.ContainsKey(y4))
            {
                int x = hashTable[y4];
                Console.WriteLine("x = " + x + ", y = " + y);
                return;
            }
        }
 
        // If no such pair exists
        Console.WriteLine(-1);
    }
 
    static void Main()
    {
        int n = 15;
        Solve(n);
    }
}

                    

Javascript

// Function to find required x & y
function solve(n) {
    const hashTable = {};
 
    // Calculate upper limit of x & y
    const upperLimit = Math.ceil(Math.pow(n, 1/4));
 
    // Store x^4 - n in the hash table
    for (let x = 0; x <= upperLimit; x++) {
        const x4 = Math.pow(x, 4);
        hashTable[x4 - n] = x;
    }
 
    // Check if there is a y such that y^4 - x^4 = n
    for (let y = 0; y <= upperLimit; y++) {
        const y4 = Math.pow(y, 4);
        if (hashTable[y4] !== undefined) {
            const x = hashTable[y4];
            console.log(`x = ${x}, y = ${y}`);
            return;
        }
    }
 
    // If no such pair exists
    console.log(-1);
}
 
// Driver code
const n = 15;
solve(n);

                    

Output
x = 2, y = 1










Time Complexity: O(N^(1/4))
Auxiliary space: O(N^(1/4))



Last Updated : 08 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads