Given a number N, the task is to find the minimum value K such that the sum of cubes of the first K natural number is greater than or equal to N.
Examples:
Input: N = 100
Output: 4
Explanation:
The sum of cubes of first 4 natural number is 100 which is equal to N = 100
Input: N = 15
Output: 3
Explanation:
The sum of cubes of first 2 natural number is 9 which is lesser than K = 15 and sum of first
3 natural number is 36 which is just greater than K. So the answer is 3.
Naive Approach: The naive approach for this problem is to run a loop from and find the sum of cubes. Whenever the sum exceeds the value of N, break from the loop.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int naive_find_x( int N)
{
int c = 0, i;
for (i = 1; i < N; i++)
{
c += i * i * i;
if (c >= N)
break ;
}
return i;
}
int main()
{
int N = 100;
cout << naive_find_x(N);
return 0;
}
|
Java
class GFG {
static int naive_find_x( int N)
{
int c = 0 , i;
for (i = 1 ; i < N; i++)
{
c += i * i * i;
if (c >= N)
break ;
}
return i;
}
public static void main(String[] args)
{
int N = 100 ;
System.out.println(naive_find_x(N));
}
}
|
Python3
def naive_find_x(N):
c = 0
for i in range ( 1 , N):
c + = i * i * i
if c> = N:
break
return i
if __name__ = = "__main__" :
N = 100
print (naive_find_x(N))
|
C#
using System;
class GFG {
static int naive_find_x( int N)
{
int c = 0, i;
for (i = 1; i < N; i++)
{
c += i * i * i;
if (c >= N)
break ;
}
return i;
}
public static void Main(String[] args)
{
int N = 100;
Console.WriteLine(naive_find_x(N));
}
}
|
Javascript
<script>
function naive_find_x(N)
{
var c = 0, i;
for (i = 1; i < N; i++)
{
c += i * i * i;
if (c >= N)
break ;
}
return i;
}
var N = 100;
document.write(naive_find_x(N));
</script>
|
Time Complexity: O(K), where K is the number which needs to be found.
Auxiliary Space: O(1) because it is using constant space for variables
Efficient Approach: One observation which needs to be made is that the sum of cubes first N natural numbers is given by the formula:
sum = ((N * (N + 1))/2)2
And, this is a monotonically increasing function. Therefore, the idea is to apply binary search to find the value of K. If the sum is greater than N for some number ‘i’, then we know that the answer is less than or equal to ‘i’. So, iterate to the left half. Else, iterate through the right half.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binary_searched_find_x( int k)
{
int l = 0;
int r = k;
int ans = 0;
while (l <= r)
{
int mid = l + (r - l) / 2;
if ( pow (((mid * (mid + 1)) / 2), 2) >= k)
{
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return ans;
}
int main()
{
int N = 100;
cout << binary_searched_find_x(N);
return 0;
}
|
Java
class GFG{
static int binary_searched_find_x( int k)
{
int l = 0 ;
int r = k;
int ans = 0 ;
while (l <= r)
{
int mid = l + (r - l) / 2 ;
if (Math.pow(((mid * (mid + 1 )) / 2 ), 2 ) >= k)
{
ans = mid;
r = mid - 1 ;
}
else
{
l = mid + 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 100 ;
System.out.println(binary_searched_find_x(N));
}
}
|
Python3
def binary_searched_find_x(k):
l = 0
r = k
ans = 0
while l< = r:
mid = l + (r - l) / / 2
if ((mid * (mid + 1 )) / / 2 ) * * 2 > = k:
ans = mid
r = mid - 1
else :
l = mid + 1
return ans
if __name__ = = "__main__" :
N = 100
print (binary_searched_find_x(N))
|
C#
using System;
class GFG{
static int binary_searched_find_x( int k)
{
int l = 0;
int r = k;
int ans = 0;
while (l <= r)
{
int mid = l + (r - l) / 2;
if (Math.Pow(((mid * (mid + 1)) / 2), 2) >= k)
{
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return ans;
}
public static void Main()
{
int N = 100;
Console.Write(binary_searched_find_x(N));
}
}
|
Javascript
<script>
function binary_searched_find_x(k)
{
var l = 0;
var r = k;
var ans = 0;
while (l <= r)
{
var mid = parseInt(l + (r - l) / 2);
if (Math.pow(((mid * (mid + 1)) / 2), 2) >= k)
{
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return ans;
}
var N = 100;
document.write(binary_searched_find_x(N));
</script>
|
Time Complexity: O(log(K)).
Auxiliary Space: O(1) because it is using constant variables