Length of Smallest subarray in range 1 to N with sum greater than a given value
Given two numbers N and S, the task is to find the length of smallest subarray in range (1, N) such that the sum of those chosen numbers is greater than S.
Examples:
Input: N = 5, S = 11
Output: 3
Explanation:
Smallest subarray with sum > 11 = {5, 4, 3}
Input: N = 4, S = 7
Output: 3
Explanation:
Smallest subarray with sum > 7 = {4, 3, 2}
Naive Approach: A brute force method is to select elements in reverse order until the sum of all the selected elements is less than or equal to the given number.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countNumber( int N, int S)
{
int countElements = 0;
int currSum = 0;
while (currSum <= S) {
currSum += N;
N--;
countElements++;
}
return countElements;
}
int main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
cout << count << endl;
return 0;
}
|
Java
class GFG
{
static int countNumber( int N, int S)
{
int countElements = 0 ;
int currSum = 0 ;
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
public static void main (String[] args)
{
int N, S;
N = 5 ;
S = 11 ;
int count = countNumber(N, S);
System.out.println(count);
}
}
|
Python
def countNumber(N, S):
countElements = 0 ;
currentSum = 0
currSum = 0 ;
while (currSum < = S) :
currSum + = N;
N = N - 1 ;
countElements = countElements + 1 ;
return countElements;
N = 5 ;
S = 11 ;
count = countNumber(N, S);
print (count) ;
|
C#
using System;
class GFG
{
static int countNumber( int N, int S)
{
int countElements = 0;
int currSum = 0;
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
public static void Main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
Console.WriteLine(count);
}
}
|
Javascript
<script>
function countNumber(N, S)
{
let countElements = 0;
let currSum = 0;
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
let N, S;
N = 5;
S = 11;
let count = countNumber(N, S);
document.write(count + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient Approach: The idea is to use Binary Search concept to solve the problem.
From the binary search concept, it is known that the concept can be applied when it is known that there is an order in the problem. That is, for every iteration, if it can be differentiated for sure that the required answer either lies in the first half or second half (i.e), there exists a pattern in the problem.
Therefore, binary search can be applied for the range in the following way:
- Initialize start = 1 and end = N.
- Find mid = start + (end – start) / 2.
- If the sum of all the elements from the last element to mid element is less than or equal to the given sum, then end = mid else start = mid + 1.
- Repeat step 2 while start is less than the end.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int usingBinarySearch( int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
int totalSum = (N * (N + 1)) / 2;
int midSum = (mid * (mid + 1)) / 2;
if ((totalSum - midSum) <= S) {
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
int main()
{
int N, S;
N = 5;
S = 11;
cout << (N - usingBinarySearch(1, N, N, S) + 1)
<< endl;
return 0;
}
|
Java
class GFG
{
static int usingBinarySearch( int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2 ;
int totalSum = (N * (N + 1 )) / 2 ;
int midSum = (mid * (mid + 1 )) / 2 ;
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1 , end, N, S);
}
public static void main (String[] args)
{
int N, S;
N = 5 ;
S = 11 ;
System.out.println(N - usingBinarySearch( 1 , N, N, S) + 1 ) ;
}
}
|
Python3
def usingBinarySearch(start, end, N, S) :
if (start > = end) :
return start;
mid = start + (end - start) / / 2 ;
totalSum = (N * (N + 1 )) / / 2 ;
midSum = (mid * (mid + 1 )) / / 2 ;
if ((totalSum - midSum) < = S) :
return usingBinarySearch(start, mid, N, S);
return usingBinarySearch(mid + 1 , end, N, S);
if __name__ = = "__main__" :
N = 5 ;
S = 11 ;
print (N - usingBinarySearch( 1 , N, N, S) + 1 ) ;
|
C#
using System;
class GFG
{
static int usingBinarySearch( int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
int totalSum = (N * (N + 1)) / 2;
int midSum = (mid * (mid + 1)) / 2;
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
public static void Main()
{
int N, S;
N = 5;
S = 11;
Console.WriteLine(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
|
Javascript
<script>
function usingBinarySearch(start, end, N, S)
{
if (start >= end)
return start;
let mid = start + (end - start) / 2;
let totalSum = (N * (N + 1)) / 2;
let midSum = (mid * (mid + 1)) / 2;
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
let N, S;
N = 5;
S = 11;
document.write((N - usingBinarySearch(
1, N, N, S) + 1) + "<br>" );
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(N) Where N is recursion stack space.
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
05 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...