The sum of two positive numbers is equal to 2 and if the sum of their cubes is least the numbers are

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:



Input: N = 28
Output: Yes
Explanation: 
Since, 28 = 27 + 1 = 33 + 13.
Therefore, the required answer is Yes.

Input: N = 34
Output: No

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:

  • Initialize an ordered map, say cubes, to store the perfect cubes of first N natural numbers in sorted order.
  • Traverse the map and check for the pair having a sum equal to N.
  • If such a pair is found having sum N, then print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

void sumOfTwoPerfectCubes(int N)

    for (int i = 1; i * i * i <= N; i++)

    map<int, int>::iterator itr;

    for (itr = cubes.begin();

         itr != cubes.end(); itr++) {

        int firstNumber = itr->first;

        int secondNumber = N - itr->first;

        if (cubes.find(secondNumber)

  public static void sumOfTwoPerfectCubes(int N)

    HashMap<Integer, Integer> cubes = new HashMap<>();

    for (int i = 1; i * i * i <= N; i++)

      cubes.put((i * i * i), i);

    Iterator<Map.Entry<Integer, Integer> > itr

      = cubes.entrySet().iterator();

      Map.Entry<Integer, Integer> entry = itr.next();

      int firstNumber = entry.getKey();

      int secondNumber = N - entry.getKey();

      if (cubes.containsKey(secondNumber))

        System.out.println("True");

    System.out.println("False");

  public static void main(String[] args)

def sumOfTwoPerfectCubes(N) :

        if secondNumber in cubes :

using System.Collections.Generic;

  public static void sumOfTwoPerfectCubes(int N)

             int> cubes = new Dictionary<int,

    for (int i = 1; i * i * i <= N; i++)

      cubes.Add((i * i * i), i);

    var val = cubes.Keys.ToList();

      int firstNumber = cubes[1];

      int secondNumber = N - cubes[1];

      if (cubes.ContainsKey(secondNumber))

static public void Main()

function sumOfTwoPerfectCubes(N)

    for (var i = 1; i * i * i <= N; i++)

    cubes.forEach((value, key) => {

        var secondNumber = N - value;

        if (cubes.has(secondNumber)) {

    document.write( "False");

Time Complexity: O(N1/3 * log(N1/3))  
Auxiliary Space: O(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)

bool sumOfTwoCubes(int n)

    long long int lo = 1, hi = (long long int)cbrt(n);

        long long int curr = (lo * lo * lo + hi * hi * hi);

static boolean sumOfTwoCubes(int n)

    int lo = 1, hi = (int)Math.cbrt(n);

        int curr = (lo * lo * lo + hi * hi * hi);

public static void main (String[] args)

        System.out.println("True");

        System.out.println("False");

    hi = round(math.pow(n, 1 / 3))

        curr = (lo * lo * lo + hi * hi * hi)

static bool sumOfTwoCubes(int n)

    int lo = 1, hi = (int)Math.Pow(n, (1.0 / 3.0));

        int curr = (lo * lo * lo + hi * hi * hi);

public static void Main (String[] args)

function sumOfTwoCubes(n)

        var curr = (lo * lo * lo + hi * hi * hi);

Time Complexity : O(log(cbrt(n))),where n is given number 

Space Complexity : O(1)


Article Tags :