Find the smallest value of N such that sum of first N natural numbers is ≥ X
Last Updated :
03 Jan, 2023
Given a positive integer X (1 ? X ? 106), the task is to find the minimum value N, such that the sum of first N natural numbers is ? X.
Examples:
Input: X = 14
Output: 5
Explanation: Sum of first 5 natural numbers is 15 which is greater than X( = 14).
- 1 + 2 = 3( < 14)
- 1 + 2 + 3 = 6( < 14)
- 1 + 2 + 3 + 4 = 10( < 15)
- 1 + 2 + 3 + 4 + 5 = 15( > 14)
Input: X = 91
Output: 13
Naive Approach: The simplest approach to solve this problem is to check every value in the range [1, X] and return the first value from this range for which the sum of the first N natural numbers is found to be ? X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isGreaterEqual( int N, int X)
{
return (N * 1LL * (N + 1) / 2) >= X;
}
int minimumPossible( int X)
{
for ( int i = 1; i <= X; i++) {
if (isGreaterEqual(i, X))
return i;
}
}
int main()
{
int X = 14;
cout << minimumPossible(X);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isGreaterEqual( int N, int X)
{
return (N * (N + 1 ) / 2 ) >= X;
}
static int minimumPossible( int X)
{
for ( int i = 1 ; i <= X; i++)
{
if (isGreaterEqual(i, X))
return i;
}
return 0 ;
}
public static void main (String[] args)
{
int X = 14 ;
System.out.print(minimumPossible(X));
}
}
|
Python3
def isGreaterEqual(N, X):
return (N * (N + 1 ) / / 2 ) > = X
def minimumPossible(X):
for i in range ( 1 , X + 1 ):
if (isGreaterEqual(i, X)):
return i
if __name__ = = '__main__' :
X = 14
print (minimumPossible(X))
|
C#
using System;
public class GFG
{
static bool isGreaterEqual( int N, int X)
{
return (N * (N + 1) / 2) >= X;
}
static int minimumPossible( int X)
{
for ( int i = 1; i <= X; i++)
{
if (isGreaterEqual(i, X))
return i;
}
return 0;
}
static public void Main ()
{
int X = 14;
Console.Write(minimumPossible(X));
}
}
|
Javascript
<script>
function isGreaterEqual(N, X)
{
return parseInt((N * (N + 1)) / 2) >= X;
}
function minimumPossible(X)
{
for (let i = 1; i <= X; i++)
{
if (isGreaterEqual(i, X))
return i;
}
}
let X = 14;
document.write(minimumPossible(X));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Efficient Method: Below is the implementation of above approach :
- The idea is to use binary search to solve this problem.
- Initialize variables low = 1, high = X and perform binary search on this range.
- Calculate mid = low + (high – low) / 2 and check if the sum of first mid numbers is greater than or equal to x or not.
- If sum ? X, store it in a variable res and set high = mid-1
- Otherwise, set low = mid + 1
- Print res, which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isGreaterEqual( int N, int X)
{
return (N * 1LL * (N + 1) / 2) >= X;
}
int minimumPossible( int X)
{
int low = 1, high = X, res = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isGreaterEqual(mid, X)) {
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return res;
}
int main()
{
int X = 14;
cout << minimumPossible(X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isGreaterEqual( int N, int X)
{
return (N * (N + 1 ) / 2 ) >= X;
}
static int minimumPossible( int X)
{
int low = 1 , high = X, res = - 1 ;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (isGreaterEqual(mid, X))
{
res = mid;
high = mid - 1 ;
}
else
low = mid + 1 ;
}
return res;
}
public static void main(String[] args)
{
int X = 14 ;
System.out.print( minimumPossible(X));
}
}
|
Python3
def isGreaterEqual(N, X):
return (N * (N + 1 ) / / 2 ) > = X;
def minimumPossible(X):
low = 1
high = X
res = - 1 ;
while (low < = high):
mid = low + (high - low) / / 2 ;
if (isGreaterEqual(mid, X)):
res = mid;
high = mid - 1 ;
else :
low = mid + 1 ;
return res
if __name__ = = "__main__" :
X = 14 ;
print (minimumPossible(X));
|
C#
using System;
class GFG{
static bool isGreaterEqual( int N, int X)
{
return (N * (N + 1) / 2) >= X;
}
static int minimumPossible( int X)
{
int low = 1, high = X, res = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (isGreaterEqual(mid, X))
{
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return res;
}
static public void Main()
{
int X = 14;
Console.Write( minimumPossible(X));
}
}
|
Javascript
<script>
function isGreaterEqual(N, X)
{
return parseInt((N * (N + 1)) / 2) >= X;
}
function minimumPossible(X)
{
let low = 1, high = X, res = -1;
while (low <= high) {
let mid = low + parseInt((high - low) / 2);
if (isGreaterEqual(mid, X)) {
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return res;
}
let X = 14;
document.write(minimumPossible(X));
</script>
|
Time Complexity: O(log(X))
Auxiliary Space: O(1)
Another Efficient Approach:
As we know that expression for the sum of the first n natural number is (n*(n+1))/2.so this expression can be very useful in finding out the value of n from the given X.
From the problem statement, it is quite clear that n * (n + 1) / 2 <= X, so first solve this equation and then use the simplified version of the approach to calculate the value of n for given X.
n * (n + 1) / 2 >=X
=> n2 + n >= 2X
=> n2 + n + (1/4 – 1/4) >= 2X
=> (n + 1/2)2 – 1/4 >= 2X
=> (n + 1/2)2 >= (2X+1/4)
=> (n + 1/2) >= sqrt(2X + 1/4)
=> n >= sqrt(2X + 1/4) – 1/2
The required value of n will be.
=> n = ceil(sqrt(2X + 1/4) – 1/2)
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
using namespace std;
int minimumPossible( int X)
{
return ceil (( pow (2 * X + 1 / 4, 0.5) - 0.5));
}
int main()
{
int X = 14;
cout << minimumPossible(X) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minimumPossible( int X)
{
return ( int )Math.ceil(( int )Math.pow(( 2 *X+ 1 / 4 ), 0.5 )- 0.5 );
}
public static void main(String[] args)
{
int X = 14 ;
System.out.print(minimumPossible(X));
}
}
|
Python3
import math
def minimumPossible(X):
return math.ceil((( 2 * X + 1 / 4 ) * * ( 0.5 )) - 1 / 2 )
if __name__ = = "__main__" :
X = 14
print (minimumPossible(X))
|
C#
using System;
class GFG
{
static int minimumPossible( int X)
{
return ( int )Math.Ceiling(( int )Math.Pow((2*X+1/4), 0.5)-0.5);
}
public static void Main( string [] args)
{
int X = 14;
Console.Write(minimumPossible(X));
}
}
|
Javascript
function minimumPossible(X)
{
return Math.ceil(((2*X+1/4)**(0.5))-1/2)
}
let X = 14
console.log(minimumPossible(X))
|
Output: 5
Time Complexity: O(log(X)), for calculating the square root of the given X value.
Auxiliary Space: O(1) as no extra space is required here.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...