Smallest value of N such that the sum of all natural numbers from K to N is at least X
Last Updated :
19 Apr, 2021
Given two positive integers X and K, the task is to find the minimum value of N possible such that the sum of all natural numbers from the range [K, N] is at least X. If no possible value of N exists, then print -1.
Examples:
Input: K = 5, X = 13
Output: 7
Explanation: The minimum possible value is 7. Sum = 5 + 6 + 7 = 18, which is at least 13.
Input: K = 3, X = 15
Output: 6
Naive Approach: The simplest approach to solve this problem is to check for every value in the range [K, X] and return the first value from this range which has sum of the first N natural numbers at least X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumNumber( int K, int X)
{
if (K > X) {
cout << "-1" ;
return ;
}
int ans = 0;
int sum = 0;
for ( int i = K; i <= X; i++) {
sum += i;
if (sum >= X) {
ans = i;
break ;
}
}
cout << ans;
}
int main()
{
int K = 5, X = 13;
minimumNumber(K, X);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void minimumNumber( int K, int X)
{
if (K > X)
{
System.out.println( "-1" );
return ;
}
int ans = 0 ;
int sum = 0 ;
for ( int i = K; i <= X; i++)
{
sum += i;
if (sum >= X)
{
ans = i;
break ;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int K = 5 , X = 13 ;
minimumNumber(K, X);
}
}
|
Python3
def minimumNumber(K, X):
if (K > X):
print ( "-1" )
return
ans = 0
sum = 0
for i in range (K, X + 1 ):
sum + = i
if ( sum > = X):
ans = i
break
print (ans)
K = 5
X = 13
minimumNumber(K, X)
|
C#
using System;
class GFG{
static void minimumNumber( int K, int X)
{
if (K > X)
{
Console.Write( "-1" );
return ;
}
int ans = 0;
int sum = 0;
for ( int i = K; i <= X; i++)
{
sum += i;
if (sum >= X)
{
ans = i;
break ;
}
}
Console.Write(ans);
}
public static void Main()
{
int K = 5, X = 13;
minimumNumber(K, X);
}
}
|
Javascript
<script>
function minimumNumber(K, X)
{
if (K > X) {
document.write( "-1" );
return ;
}
let ans = 0;
let sum = 0;
for (let i = K; i <= X; i++) {
sum += i;
if (sum >= X) {
ans = i;
break ;
}
}
document.write(ans);
}
let K = 5, X = 13;
minimumNumber(K, X);
</script>
|
Time Complexity: O(N – K)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using Binary Search. Follow the steps below to solve the given problem:
- Initialize a variable, say res as -1, to store the smallest possible value of N that satisfies the given conditions.
- Initialize two variables, say low as K, and high as X, and perform Binary Search on this range by performing the following steps:
- Find the value of mid as low + (high – low) / 2.
- If the sum of natural numbers from K to mid is greater than or equal to X or not.
- If found to be true, then update res as mid and set high = (mid – 1). Otherwise, update the low to (mid + 1).
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isGreaterEqual( int N, int K, int X)
{
return ((N * 1LL * (N + 1) / 2)
- ((K - 1) * 1LL * K / 2))
>= X;
}
void minimumNumber( int K, int X)
{
if (K > X) {
cout << "-1" ;
return ;
}
int low = K, high = X, res = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isGreaterEqual(mid, K, X)) {
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
cout << res;
}
int main()
{
int K = 5, X = 13;
minimumNumber(K, X);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static boolean isGreaterEqual( int N, int K, int X)
{
return ((N * 1L * (N + 1 ) / 2 ) -
((K - 1 ) * 1L * K / 2 )) >= X;
}
static void minimumNumber( int K, int X)
{
if (K > X)
{
System.out.println( "-1" );
return ;
}
int low = K, high = X, res = - 1 ;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (isGreaterEqual(mid, K, X))
{
res = mid;
high = mid - 1 ;
}
else
low = mid + 1 ;
}
System.out.println(res);
}
public static void main(String[] args)
{
int K = 5 , X = 13 ;
minimumNumber(K, X);
}
}
|
Python3
def isGreaterEqual(N, K, X):
return (((N * (N + 1 ) / / 2 )
- ((K - 1 ) * K / / 2 ))
> = X)
def minimumNumber(K, X):
if (K > X):
print ( "-1" )
return
low = K
high = X
res = - 1
while (low < = high):
mid = low + ((high - low) / / 2 )
if (isGreaterEqual(mid, K, X)):
res = mid
high = mid - 1
else :
low = mid + 1
print (res)
K = 5
X = 13
minimumNumber(K, X)
|
C#
using System;
class GFG{
static bool isGreaterEqual( int N, int K, int X)
{
return ((N * 1L * (N + 1) / 2) -
((K - 1) * 1L * K / 2)) >= X;
}
static void minimumNumber( int K, int X)
{
if (K > X)
{
Console.Write( "-1" );
return ;
}
int low = K, high = X, res = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (isGreaterEqual(mid, K, X))
{
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
Console.WriteLine(res);
}
public static void Main()
{
int K = 5, X = 13;
minimumNumber(K, X);
}
}
|
Javascript
<script>
function isGreaterEqual(N, K, X)
{
return ((N * parseInt((N + 1) / 2))
- ((K - 1) * parseInt(K / 2)))
>= X;
}
function minimumNumber(K, X)
{
if (K > X) {
document.write( "-1" );
return ;
}
let low = K, high = X, res = -1;
while (low <= high) {
let mid = low + parseInt((high - low) / 2);
if (isGreaterEqual(mid, K, X)) {
res = mid;
high = mid - 1;
}
else
low = mid + 1;
}
document.write(res);
}
let K = 5, X = 13;
minimumNumber(K, X);
</script>
|
Time Complexity: O(log(X – K))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...