Minimum value of K such that sum of cubes of first K natural number is greater than equal to N

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to determine the 
// minimum value of K such that the 
// sum of cubes of first K 
// natural number is greater than 
// or equal to N
#include <bits/stdc++.h>
using namespace std;
  
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
int naive_find_x(int N)
{
  
    // Variable to store the
    // sum of cubes
    int c = 0, i;
  
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
        c += i * i * i;
              
        // If C is just greater then
        // N, then break the loop
        if (c >= N)
            break;
    }
    return i;
}
  
// Driver code
int main()
{
    int N = 100;
    cout << naive_find_x(N);
    return 0;
}
  
// This code is contributed by sapnasingh4991

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to determine the 
// minimum value of K such that the 
// sum of cubes of first K 
// natural number is greater than 
// or equal to N
class GFG {
      
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int naive_find_x(int N)
{
  
    // Variable to store the
    // sum of cubes
    int c = 0, i;
  
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
       c += i * i * i;
         
       // If C is just greater then
       // N, then break the loop
       if (c >= N)
           break;
    }
    return i;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 100;
      
    System.out.println(naive_find_x(N));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to determine the 
# minimum value of K such that the 
# sum of cubes of first K 
# natural number is greater than 
# or equal to N
  
# Function to determine the 
# minimum value of K such that the 
# sum of cubes of first K 
# natural number is greater than 
# or equal to N
def naive_find_x(N):
  
    # Variable to store the 
    # sum of cubes
    c = 0
  
    # Loop to find the number
    # K
    for i in range(1, N):
  
        c += i*i*i
  
        # If C is just greater then
        # N, then break the loop
        if c>= N:
            break
  
    return i
  
# Driver code
if __name__ == "__main__":
      
    N = 100
    print(naive_find_x(N))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to determine the 
// minimum value of K such that the 
// sum of cubes of first K 
// natural number is greater than 
// or equal to N
using System;
  
class GFG {
      
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int naive_find_x(int N)
{
  
    // Variable to store the
    // sum of cubes
    int c = 0, i;
  
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
    c += i * i * i;
          
    // If C is just greater then
    // N, then break the loop
    if (c >= N)
        break;
    }
    return i;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 100;
      
    Console.WriteLine(naive_find_x(N));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

4

Time Complexity: O(K), where K is the number which needs to be found.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to determine the 
// minimum value of K such that 
// the sum of cubes of first K 
// natural number is greater than 
// or equal to N
#include <bits/stdc++.h>
using namespace std;
      
// Function to determine the
// minimum value of K such that 
// the sum of cubes of first K
// natural number is greater than
// or equal to N
int binary_searched_find_x(int k) 
{
      
    // Left bound
    int l = 0;
  
    // Right bound
    int r = k;
  
    // Variable to store the
    // answer
    int ans = 0;
  
    // Applying binary search
    while (l <= r) 
    {
          
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
  
        if (pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
              
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        
        else
        {
  
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
  
// Driver code
int main() 
{
    int N = 100;
      
    cout << binary_searched_find_x(N);
    return 0;
}
  
// This code is contributed by shubhamsingh10

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to determine the 
// minimum value of K such that the 
// sum of cubes of first K 
// natural number is greater than 
// or equal to N
class GFG{
      
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k) 
{
  
    // Left bound
    int l = 0;
  
    // Right bound
    int r = k;
  
    // Variable to store the
    // answer
    int ans = 0;
  
    // Applying binary search
    while (l <= r) 
    {
  
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
  
        if (Math.pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
  
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        
        else 
        {
  
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args) 
{
    int N = 100;
    System.out.println(binary_searched_find_x(N));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to determine the 
# minimum value of K such that the 
# sum of cubes of first K 
# natural number is greater than 
# or equal to N
  
# Function to determine the 
# minimum value of K such that the 
# sum of cubes of first K 
# natural number is greater than 
# or equal to N
def binary_searched_find_x(k):
  
    # Left bound
    l = 0 
  
    # Right bound
    r =
  
    # Variable to store the 
    # answer
    ans = 0 
  
    # Applying binary search
    while l<= r:
  
        # Calculating mid value 
        # of the range
        mid = l+(r-l)//2 
  
        if ((mid*(mid + 1))//2)**2>= k:
  
             # If the sum of cubes of 
             # first mid natural numbers
             # is greater than equal to N
             # iterate the left half
             ans = mid
             r = mid-1 
  
        else:
  
             # Sum of cubes of first 
             # mid natural numbers is 
             # less than N, then move
             # to the right segment
             l = mid + 1     
  
    return ans    
  
# Driver code
if __name__ == "__main__":
    N = 100
    print(binary_searched_find_x(N))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to determine the 
// minimum value of K such that the 
// sum of cubes of first K 
// natural number is greater than 
// or equal to N
using System;
class GFG{
      
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k) 
{
  
    // Left bound
    int l = 0;
  
    // Right bound
    int r = k;
  
    // Variable to store the
    // answer
    int ans = 0;
  
    // Applying binary search
    while (l <= r) 
    {
  
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
  
        if (Math.Pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
  
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        
        else
        {
  
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
  
// Driver code
public static void Main() 
{
    int N = 100;
    Console.Write(binary_searched_find_x(N));
}
}
  
// This code is contributed by Nidhi_biet

chevron_right


Output:

4

Time Complexity: O(log(K)).

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.