Length of Smallest subarray in range 1 to N with sum greater than a given value
Last Updated :
05 Mar, 2023
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++
// C++ implementation of the above implementation
#include <bits/stdc++.h>
using namespace std;
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S) {
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
int main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
cout << count << endl;
return 0;
}
Java
// Java implementation of the above implementation
class GFG
{
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
static int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
public static void main (String[] args)
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
System.out.println(count);
}
}
// This code is contributed by AnkitRai01
Python
# Python implementation of the above implementation
# Function to return the count
# of minimum elements such that
# the sum of those elements is > S.
def countNumber(N, S):
countElements = 0;
currentSum = 0
currSum = 0;
# Loop from N to 1 to add the numbers
# and check the condition.
while (currSum <= S) :
currSum += N;
N = N - 1;
countElements=countElements + 1;
return countElements;
# Driver code
N = 5;
S = 11;
count = countNumber(N, S);
print(count) ;
# This code is contributed by Shivi_Aggarwal
C#
// C# implementation of the above implementation
using System;
class GFG
{
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
static int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
public static void Main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
Console.WriteLine(count);
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// JavaScript implementation of the above implementation
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
function countNumber(N, S)
{
let countElements = 0;
// Initialize currentSum = 0
let currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
let N, S;
N = 5;
S = 11;
let count = countNumber(N, S);
document.write(count + "<br>");
// This code is contributed by Surbhi Tyagi.
</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++
// C++ implementation of the above approach.
#include <iostream>
using namespace std;
// Function to do a binary search
// on a given range.
int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S) {
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
int main()
{
int N, S;
N = 5;
S = 11;
cout << (N - usingBinarySearch(1, N, N, S) + 1)
<< endl;
return 0;
}
Java
// Java implementation of the above approach.
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void main (String[] args)
{
int N, S;
N = 5;
S = 11;
System.out.println(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach.
# Function to do a binary search
# on a given range.
def usingBinarySearch(start, end, N, S) :
if (start >= end) :
return start;
mid = start + (end - start) // 2;
# Total sum is the sum of N numbers.
totalSum = (N * (N + 1)) // 2;
# Sum until mid
midSum = (mid * (mid + 1)) // 2;
# If remaining sum is < the required value,
# then the required number is in the right half
if ((totalSum - midSum) <= S) :
return usingBinarySearch(start, mid, N, S);
return usingBinarySearch(mid + 1, end, N, S);
# Driver code
if __name__ == "__main__" :
N = 5;
S = 11;
print(N - usingBinarySearch(1, N, N, S) + 1) ;
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach.
using System;
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void Main()
{
int N, S;
N = 5;
S = 11;
Console.WriteLine(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript implementation of the above approach.
// Function to do a binary search
// on a given range.
function usingBinarySearch(start, end, N, S)
{
if (start >= end)
return start;
let mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
let totalSum = (N * (N + 1)) / 2;
// Sum until mid
let midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
let N, S;
N = 5;
S = 11;
document.write((N - usingBinarySearch(
1, N, N, S) + 1) + "<br>");
// This code is contributed by Mayank Tyagi
</script>
Time Complexity: O(log N)
Auxiliary Space: O(N) Where N is recursion stack space.
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Smallest subarray of size greater than K with sum greater than a given value Given an array, arr[] of size N, two positive integers K and S, the task is to find the length of the smallest subarray of size greater than K, whose sum is greater than S. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 1, S = 8Output: 2Explanation: Smallest subarray with sum greater than S(=8) is {4
15 min read
Smallest subarray with sum greater than a given value Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum strictly greater than x.Examples:Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]Output: 3Explanation: Minimum length subarray is [4, 45, 6]Input: x = 100, arr[] = [1, 10, 5, 2, 7]Output: 0Explanation: N
15+ min read
Smallest subarray with sum greater than or equal to K Given an array A[] consisting of N integers and an integer K, the task is to find the length of the smallest subarray with sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: A[] = {2, -1, 2}, K = 3 Output: 3 Explanation: Sum of the given array is 3. Hence, the sm
15+ min read
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 Given an array A[] consisting of N positive integers and an integer K, the task is to find the length of the smallest subarray with a sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: arr[] = {3, 1, 7, 1, 2}, K = 11Output: 3Explanation:The smallest subarray with
15+ min read
Longest Subarray with Sum greater than Equal to Zero Given an array of N integers. The task is to find the maximum length subarray such that the sum of all its elements is greater than or equal to 0. Examples: Input: arr[]= {-1, 4, -2, -5, 6, -8} Output: 5 Explanation: {-1, 4, -2, -5, 6} forms the longest subarray with sum=2. Input: arr[]={-5, -6} Out
12 min read