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.
Input: N = 97 Output: Yes Explanation: Summation of the biquadrates of 2 and 3 is 97, i.e. 24 + 34 = 16 + 81 = 97(= N).
Naïve Approach: 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)
Efficient Approach: 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:
Initialize two pointers, say i as 0 and j as .
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.
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:
Please Login to comment...