Maximize X such that sum of numbers in range [1, X] is at most K
Given two integers N and an integer K, the task is to find the count of integers less than or equal to N, such that the sum of the natural numbers up to that integer is less than or equal to K.
Examples:
Input: N = 5, K = 10
Output: 4
Explanation:
The integers 1, 2, 3, and 4 satisfy the condition.
- The sum of natural numbers up to integer 1 is equal to 1. Which is less than 10.
- The sum of natural numbers up to integer 2 is equal to (1+2 =) 3. Which is less than 10.
- The sum of natural numbers up to integer 3 is equal to (1+2+3 =) 6. Which is less than 10.
- The sum of natural numbers up to integer 4 is equal to (1+2+3+4 =) 10. Which is equal to 10.
Input: N=3, K=0
Output: 0
Approach: The simplest approach is to traverse over the range [1, N] and count the number of elements whose sum is less than or equal to K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Count( int N, int K)
{
if (K == 0)
return 0;
int sum = 0;
int res = 0;
for ( int i = 1; i <= N; i++) {
sum += i;
if (sum <= K)
res++;
else
break ;
}
return res;
}
int main()
{
int N = 6, K = 14;
cout << Count(N, K);
return 0;
}
|
Java
public class GFG
{
static int Count( int N, int K)
{
if (K == 0 )
return 0 ;
int sum = 0 ;
int res = 0 ;
for ( int i = 1 ; i <= N; i++)
{
sum += i;
if (sum <= K)
res++;
else
break ;
}
return res;
}
public static void main(String args[])
{
int N = 6 , K = 14 ;
System.out.println(Count(N, K));
}
}
|
Python3
def Count(N, K):
if (K = = 0 ):
return 0
sum = 0
res = 0
for i in range ( 1 , N + 1 , 1 ):
sum + = i
if ( sum < = K):
res + = 1
else :
break
return res
if __name__ = = '__main__' :
N = 6
K = 14
print (Count(N, K))
|
C#
using System;
class GFG {
static int Count( int N, int K)
{
if (K == 0)
return 0;
int sum = 0;
int res = 0;
for ( int i = 1; i <= N; i++) {
sum += i;
if (sum <= K)
res++;
else
break ;
}
return res;
}
public static void Main()
{
int N = 6, K = 14;
Console.WriteLine(Count(N, K));
}
}
|
Javascript
<script>
function Count(N, K) {
if (K == 0)
return 0;
let sum = 0;
let res = 0;
for (let i = 1; i <= N; i++) {
sum += i;
if (sum <= K)
res++;
else
break ;
}
return res;
}
let N = 6, K = 14;
document.write(Count(N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using a binary search algorithm. Follow the steps below to solve the problem:
- Initialize a variable say res as 0 to store the result.
- Also, initialize two variables, say low and high, as 1 and N respectively.
- Iterate until low is less than or equal to high and perform the following steps:
- Find the mid-value of the range [low, high] and store it in a variable, say mid.
- Calculate the sum of the natural numbers up to mid and, store it in a variable, say sum.
- If the sum is less than or equal to K, then update res to max(res, mid) and assign mid+1 to low.
- Otherwise, assign mid-1 to high.
- Finally, after completing the above steps, print the value of res as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Count( int N, int K)
{
if (K == 0)
return 0;
int res = 0;
int low = 1, high = N;
while (low <= high) {
int mid = (low + high) / 2;
int sum = (mid * mid + mid) / 2;
if (sum <= K) {
res = max(res, mid);
low = mid + 1;
}
else {
high = mid - 1;
}
}
return res;
}
int main()
{
int N = 6, K = 14;
cout << Count(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Count( int N, int K)
{
if (K == 0 )
return 0 ;
int res = 0 ;
int low = 1 , high = N;
while (low <= high) {
int mid = (low + high) / 2 ;
int sum = (mid * mid + mid) / 2 ;
if (sum <= K)
{
res = Math.max(res, mid);
low = mid + 1 ;
}
else
{
high = mid - 1 ;
}
}
return res;
}
public static void main(String[] args)
{
int N = 6 , K = 14 ;
System.out.println(Count(N, K));
}
}
|
Python3
def Count(N, K):
if (K = = 0 ):
return 0
res = 0
low = 1
high = N
while (low < = high):
mid = (low + high) / / 2
sum = (mid * mid + mid) / / 2
if ( sum < = K):
res = max (res, mid)
low = mid + 1
else :
high = mid - 1
return res
if __name__ = = '__main__' :
N = 6
K = 14
print (Count(N, K))
|
C#
using System;
class GFG{
static int Count( int N, int K)
{
if (K == 0)
return 0;
int res = 0;
int low = 1, high = N;
while (low <= high)
{
int mid = (low + high) / 2;
int sum = (mid * mid + mid) / 2;
if (sum <= K)
{
res = Math.Max(res, mid);
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return res;
}
public static void Main(String[] args)
{
int N = 6, K = 14;
Console.Write (Count(N, K));
}
}
|
Javascript
<script>
function Count( N, K)
{
if (K == 0)
return 0;
var res = 0;
var low = 2, high = N;
while (low <= high) {
var mid = (low + high) / 2;
var sum = (mid * mid + mid) / 2;
if (sum <= K)
{
res = Math.max(res, mid);
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return res;
}
var N = 6, K = 14;
document.write(Count(N, K));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
21 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...