Given an integer N, the task is to check if N can be represented as the sum of two positive perfect cubes or not. Examples:
Approach: The idea is to store the perfect cubes of all numbers from 1 to cubic root of N in a Map and check if N can be represented as the sum of two numbers present in the Map or not. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Time Complexity: O(N1/3 * log(N1/3)) Approach 2 : Using two Pointers: We will declare lo to 1 and hi to cube root of n(the given number), then by (lo<=hi) this condition, if current is smaller than n we will increment the lo and in other hand if it is greater then decrement the hi, where current is (lo*lo*lo + hi*hi*hi)
Time Complexity : O(log(cbrt(n))),where n is given number Space Complexity : O(1) Article Tags : |