Check if a number can be represented as sum of two positive perfect cubes | C ++ program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfTwoPerfectCubes ( int N ) { map < int , int > cubes ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes [ i * i * i ] = 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 ) != cubes . end ( ) ) { cout << " True " ; return ; } } cout << " False " ; } int main ( ) { int N = 28 ; sumOfTwoPerfectCubes ( N ) ; return 0 ; }