Minimum number of cubes whose sum equals to given number N
Given an integer n, the task is to find the minimum number of cubes whose sum equals N.
Examples:
Input: N = 496
Output: 3
43 + 63 + 63 = 496
Note that 13 + 33 + 53 + 73 = 496 but it requires 4 cubes.
Input: N = 15
Output: 8
Naive approach: Write a recursive method that will take every perfect cube less than N say X as part of the summation and then recur for the number of cubes required for the remaining sum N – X. The time complexity of this solution is exponential.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int MinOfCubed( int k)
{
if (k < 8)
return k;
int res = k;
for ( int i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
int main()
{
int num = 15;
cout << MinOfCubed(num);
return 0;
}
|
Java
class GFG {
static int MinOfCubed( int k)
{
if (k < 8 )
return k;
int res = k;
for ( int i = 1 ; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = Math.min(res, MinOfCubed(k - (i * i * i)) + 1 );
}
return res;
}
public static void main(String[] args)
{
int num = 15 ;
System.out.println(MinOfCubed(num));
}
}
|
Python3
def MinOfCubed(k):
if (k < 8 ):
return k;
res = k;
for i in range ( 1 , k + 1 ):
if ((i * i * i) > k):
return res;
res = min (res, MinOfCubed(k - (i * i * i)) + 1 );
return res;
num = 15 ;
print (MinOfCubed(num));
|
C#
using System;
class GFG {
static int MinOfCubed( int k)
{
if (k < 8)
return k;
int res = k;
for ( int i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = Math.Min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
static public void Main()
{
int num = 15;
Console.WriteLine(MinOfCubed(num));
}
}
|
PHP
<?php
function MinOfCubed( $k )
{
if ( $k < 8)
return $k ;
$res = $k ;
for ( $i = 1; $i <= $k ; $i ++)
{
if (( $i * $i * $i ) > $k )
return $res ;
$res = min( $res , MinOfCubed( $k - ( $i * $i * $i )) + 1);
}
return $res ;
}
$num = 15;
echo MinOfCubed( $num );
?>
|
Javascript
<script>
function MinOfCubed(k)
{
if (k < 8)
return k;
let res = k;
for (let i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = Math.min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
let num = 15;
document.write(MinOfCubed(num));
</script>
|
Efficient approach: If we draw the complete recursion tree for the above solution, we can see that many sub-problems are solved, again and again, so we can see that this problem has the overlapping sub-problems property. This leads us to solve the problem using the Dynamic Programming paradigm.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinOfCubedDP( int k)
{
vector< int > DP(k+1);
int j = 1, t = 1;
DP[0] = 0;
for ( int i = 1; i <= k; i++) {
DP[i] = INT_MAX;
while (j <= i) {
if (j == i)
DP[i] = 1;
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
t++;
j = t * t * t;
}
t = j = 1;
}
return DP[k];
}
int main()
{
int num = 15;
cout << MinOfCubedDP(num);
return 0;
}
|
Java
class GFG {
static int MinOfCubedDP( int k)
{
int [] DP = new int [k + 1 ];
int j = 1 , t = 1 ;
DP[ 0 ] = 0 ;
for ( int i = 1 ; i <= k; i++) {
DP[i] = Integer.MAX_VALUE;
while (j <= i) {
if (j == i)
DP[i] = 1 ;
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1 ;
t++;
j = t * t * t;
}
t = j = 1 ;
}
return DP[k];
}
public static void main(String[] args)
{
int num = 15 ;
System.out.println(MinOfCubedDP(num));
}
}
|
Python3
import sys
def MinOfCubedDP(k):
DP = [ 0 ] * (k + 1 );
j = 1 ;
t = 1 ;
DP[ 0 ] = 0 ;
for i in range ( 1 , k + 1 ):
DP[i] = sys.maxsize;
while (j < = i):
if (j = = i):
DP[i] = 1 ;
elif (DP[i] > DP[i - j]):
DP[i] = DP[i - j] + 1 ;
t + = 1 ;
j = t * t * t;
t = j = 1 ;
return DP[k];
num = 15 ;
print (MinOfCubedDP(num));
|
C#
using System;
class GFG {
static int MinOfCubedDP( int k)
{
int [] DP = new int [k + 1];
int j = 1, t = 1;
DP[0] = 0;
for ( int i = 1; i <= k; i++) {
DP[i] = int .MaxValue;
while (j <= i) {
if (j == i)
DP[i] = 1;
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
t++;
j = t * t * t;
}
t = j = 1;
}
return DP[k];
}
public static void Main()
{
int num = 15;
Console.WriteLine(MinOfCubedDP(num));
}
}
|
PHP
<?php
function MinOfCubedDP( $k )
{
$DP = array ( $k + 1);
$j = 1; $t = 1;
$DP [0] = 0;
for ( $i = 1; $i <= $k ; $i ++)
{
$DP [ $i ] = PHP_INT_MAX;
while ( $j <= $i )
{
if ( $j == $i )
$DP [ $i ] = 1;
else if ( $DP [ $i ] > $DP [ $i - $j ])
$DP [ $i ] = $DP [ $i - $j ] + 1;
$t ++;
$j = $t * $t * $t ;
}
$t = $j = 1;
}
return $DP [ $k ];
}
$num = 15;
echo (MinOfCubedDP( $num ));
?>
|
Javascript
<script>
function MinOfCubedDP(k)
{
let DP = new Array(k + 1);
DP.fill(0);
let j = 1, t = 1;
DP[0] = 0;
for (let i = 1; i <= k; i++) {
DP[i] = Number.MAX_VALUE;
while (j <= i) {
if (j == i)
DP[i] = 1;
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
t++;
j = t * t * t;
}
t = j = 1;
}
return DP[k];
}
let num = 15;
document.write(MinOfCubedDP(num));
</script>
|
At the first glance, it seems that the algorithm works in polynomial time because we have two nested loops, the outer loop takes O(n), and the inner loop takes O(n^(1/3)). So the overall algorithm takes O(n*n^(1/3)). But what is the complexity as a function of input length? To represent a number in size of n we need m=log(n) – (log of base 2) bits. In this case n=2^m. If we set 2^m as n in the formula O(n*n^(1/3)), we see that the time complexity is still exponential. This algorithm is called Pseudo-polynomial.
Last Updated :
05 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...