 GeeksforGeeks App
Open App Browser
Continue

# Check if a number can be represented as sum of two positive perfect biquadrates

Given a positive integer N, the task is to check if N can be written as the sum of two perfect biquadrates or not i.e., (N = X4 + Y4), where X and Y are non-negative integers. If it is possible, then print Yes. Otherwise, print No.

Examples:

Input: N = 97
Output: Yes
Explanation: Summation of the biquadrates of 2 and 3 is 97, i.e. 24 + 34 = 16 + 81 = 97(= N).
Input: N = 7857
Output: Yes

### Approach 1: Naïve

The simplest approach to solve the given problem is to consider all possible combinations of integers X and Y and check if the sum of their biquadrates equals N or not. If found to be true, then print Yes. Otherwise, print No.

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

### Approach 2: Binary Search

we can use binary search to optimize the search for pairs of biquadrates that sum up to the given number. In this program, we first create a vector of all possible biquadrates up to the square root of the square root of N. Then, we iterate through this vector and for each biquadrate, we check if its complement (i.e., N minus the biquadrate) exists in the vector using binary search. If we find such a complement, we return “Yes”. If we don’t find any such pair, we return “No”.

## C++

 #include using namespace std; // Function to check if the given number// N can be expressed as the sum of two// biquadrates or notstring sumOfTwoBiquadrates(int N){    // Base Case    if (N == 0)        return "Yes";     // Find the range to traverse    int j = floor(sqrt(sqrt(N)));    int i = 1;     // Store all possible biquadrates in a vector    vector<int> biquadrates;    for (int x = 1; x <= j; x++) {        int biquadrate = x * x * x * x;        biquadrates.push_back(biquadrate);    }     // Use binary search to find the pair of biquadrates    // that sum up to N    for (int i = 0; i < biquadrates.size(); i++) {        int complement = N - biquadrates[i];        if (binary_search(biquadrates.begin(),                          biquadrates.end(), complement)) {            return "Yes";        }    }     // If no such pairs exist    return "No";} // Driver Codeint main(){    int N = 7857;    cout << sumOfTwoBiquadrates(N);     return 0;}

## Java

 import java.util.*; public class Main {    // Function to check if the given number    // N can be expressed as the sum of two    // biquadrates or not    public static String sumOfTwoBiquadrates(int N)    {        // Base Case        if (N == 0)            return "Yes";         // Find the range to traverse        int j = (int)Math.floor(Math.sqrt(Math.sqrt(N)));        int i = 1;         // Store all possible biquadrates in a vector        List biquadrates            = new ArrayList();        for (int x = 1; x <= j; x++) {            int biquadrate = x * x * x * x;            biquadrates.add(biquadrate);        }         // Use binary search to find the pair of biquadrates        // that sum up to N        for (i = 0; i < biquadrates.size(); i++) {            int complement = N - biquadrates.get(i);            if (Collections.binarySearch(biquadrates,                                         complement)                >= 0) {                return "Yes";            }        }         // If no such pairs exist        return "No";    }     // Driver Code    public static void main(String[] args)    {        int N = 7857;        System.out.println(sumOfTwoBiquadrates(N));    }}

## Python3

 import math # Function to check if the given number# N can be expressed as the sum of two# biquadrates or notdef sumOfTwoBiquadrates(N):    # Base Case    if N == 0:        return "Yes"     # Find the range to traverse    j = math.floor(math.sqrt(math.sqrt(N)))    i = 1     # Store all possible biquadrates in a list    biquadrates = []    for x in range(1, j+1):        biquadrate = x * x * x * x        biquadrates.append(biquadrate)     # Use binary search to find the pair of biquadrates    # that sum up to N    for i in range(len(biquadrates)):        complement = N - biquadrates[i]        if complement in biquadrates:            return "Yes"     # If no such pairs exist    return "No" # Driver CodeN = 7857print(sumOfTwoBiquadrates(N))

## Javascript

 function sumOfTwoBiquadrates(N) {    // Base Case    if (N === 0) {        return "Yes";    }     // Find the range to traverse    const j = Math.floor(Math.sqrt(Math.sqrt(N)));    let i = 1;     // Store all possible biquadrates in a vector    const biquadrates = [];    for (let x = 1; x <= j; x++) {        const biquadrate = x * x * x * x;        biquadrates.push(biquadrate);    }     // Use binary search to find the pair of biquadrates    // that sum up to N    for (let i = 0; i < biquadrates.length; i++) {        const complement = N - biquadrates[i];        if (biquadrates.includes(complement)) {            return "Yes";        }    }     // If no such pairs exist    return "No";} // Driver Codeconst N = 7857;console.log(sumOfTwoBiquadrates(N));

## C#

 // Importing required librariesusing System;using System.Collections.Generic; namespace ConsoleApp {class Program {    // Function to check if the given number    // N can be expressed as the sum of two    // biquadrates or not    static string SumOfTwoBiquadrates(int N)    {        // Base Case        if (N == 0)            return "Yes";        // Find the range to traverse        int j = (int)Math.Floor(Math.Sqrt(Math.Sqrt(N)));        int i = 1;         // Store all possible biquadrates in a list        List<int> biquadrates = new List<int>();        for (int x = 1; x <= j; x++) {            int biquadrate = x * x * x * x;            biquadrates.Add(biquadrate);        }         // Use binary search to find the pair of biquadrates        // that sum up to N        foreach(int biquadrate in biquadrates)        {            int complement = N - biquadrate;            if (biquadrates.BinarySearch(complement) >= 0) {                return "Yes";            }        }         // If no such pairs exist        return "No";    }     // Main Function    static void Main(string[] args)    {        int N = 7857;        Console.WriteLine(SumOfTwoBiquadrates(N));    }}}// This code is contributed by sarojmcy2e

Output:

Yes

Time Complexity: O(sqrt(N) * logN), where N is the given number.
Auxiliary Space: O(1)

### Approach 3: Two Pointer (Efficient)

The above approach can also be optimized by using the two-pointer approach, the idea is to find the two numbers over the range .  Follow the below step to solve this problem:

1. Initialize two pointers, say i as 0 and j as .
2. Iterate a loop until j becomes less than i, and perform the following steps:
• If the value of j4 is N and i4 is N, then print Yes.
• If the value of (i4 + i4) or (j4 + j4) or (i4 + j4) is N, then print Yes.
• If the value of (i4 + j4) is less than N, then increment the pointer i by 1. Otherwise, decrement the pointer j by 1.
3. After completing the above steps, if the loops terminate, then print No as there exists no such pairs satisfying the given criteria.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to check if the given number// N can be expressed as the sum of two// biquadrates or notstring sumOfTwoBiquadrates(int N){    // Base Case    if (N == 0)        return "Yes";     // Find the range to traverse    int j = floor(sqrt(sqrt(N)));    int i = 1;     while (i <= j) {         // If x & y exists        if (j * j * j * j == N)            return "Yes";        if (i * i * i * i == N)            return "Yes";        if (i * i * i * i + j * j * j * j == N)            return "Yes";         // If sum of powers of i and j        // of 4 is less than N, then        // increment the value of i        if (i * i * i * i + j * j * j * j < N)            i++;         // Otherwise, decrement the        // value of j        else            j--;    }     // If no such pairs exist    return "No";} // Driver Codeint main(){    int N = 7857;    cout << sumOfTwoBiquadrates(N);     return 0;}

## Java

 // Java program for the above approachpublic class GFG{   // Function to check if the given number// N can be expressed as the sum of two// biquadrates or notstatic String sumOfTwoBiquadrates(int N){       // Base Case    if (N == 0)        return "Yes";     // Find the range to traverse    int j = (int)Math.floor((double)(Math.sqrt((int)(Math.sqrt(N)))));      int i = 1;     while (i <= j) {         // If x & y exists        if (j * j * j * j == N)            return "Yes";        if (i * i * i * i == N)            return "Yes";        if (i * i * i * i                + j * j * j * j            == N)            return "Yes";         // If sum of powers of i and j        // of 4 is less than N, then        // increment the value of i        if (i * i * i * i                + j * j * j * j            < N)            i++;         // Otherwise, decrement the        // value of j        else            j--;    }     // If no such pairs exist    return "No";} // Driver Codepublic static void main(String []args){    int N = 7857;    System.out.println(sumOfTwoBiquadrates(N));    }} // This code is contributed by AnkThon

## Python3

 # python program for the above approachimport math # Function to check if the given number# N can be expressed as the sum of two# biquadrates or not  def sumOfTwoBiquadrates(N):     # Base Case    if (N == 0):        return "Yes"     # Find the range to traverse    j = int(math.sqrt(math.sqrt(N)))    i = 1     while (i <= j):         # If x & y exists        if (j * j * j * j == N):            return "Yes"        if (i * i * i * i == N):            return "Yes"        if (i * i * i * i + j * j * j * j == N):            return "Yes"         # If sum of powers of i and j        # of 4 is less than N, then        # increment the value of i        if (i * i * i * i + j * j * j * j < N):            i += 1         # Otherwise, decrement the        # value of j        else:            j -= 1     # If no such pairs exist    return "No"  # Driver Codeif __name__ == "__main__":     N = 7857    print(sumOfTwoBiquadrates(N)) # This code is contributed by rakeshsahni

## Javascript

 

## C#

 // C# program for the above approachusing System;class GFG {     // Function to check if the given number    // N can be expressed as the sum of two    // biquadrates or not    static string sumOfTwoBiquadrates(int N)    {         // Base Case        if (N == 0)            return "Yes";         // Find the range to traverse        int j = (int)Math.Floor(            (double)(Math.Sqrt((int)(Math.Sqrt(N)))));         int i = 1;         while (i <= j) {             // If x & y exists            if (j * j * j * j == N)                return "Yes";            if (i * i * i * i == N)                return "Yes";            if (i * i * i * i + j * j * j * j == N)                return "Yes";             // If sum of powers of i and j            // of 4 is less than N, then            // increment the value of i            if (i * i * i * i + j * j * j * j < N)                i++;             // Otherwise, decrement the            // value of j            else                j--;        }         // If no such pairs exist        return "No";    }     // Driver Code    public static void Main()    {        int N = 7857;        Console.WriteLine(sumOfTwoBiquadrates(N));    }} // This code is contributed by ukasp.

Output:

Yes

Time Complexity: Auxiliary Space: O(1)

My Personal Notes arrow_drop_up