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 = 8
Output: 2
Explanation:
Smallest subarray with sum greater than S(=8) is {4, 5}
Therefore, the required output is 2.Input: arr[] = {1, 3, 5, 1, 8, 2, 4}, K= 2, S= 13
Output: 3
Approach: The problem can be solved using Sliding Window Technique. Follow the steps below to solve the problem:
- Initialize two variables say, start = 0 and end = 0 to store the first and last index of current subarray respectively.
- Traverse the array, arr[] and by incrementing end and adding arr[end] to the sum of the current subarray.
- If sum of all the elements of the current subarray is less than or equal to S or the length of the current subarray is less than or equal to K, then increment the length of current subarray(end++).
- Otherwise, decrement the length of current subarray by removing the first element of the current subarray (start -= 1). Update the minimum length of required subarray found till now by comparing it with the length of the current subarray.
- Finally, print the minimum length of required subarray obtained that satisfies the conditions.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the length of the // smallest subarray of size > K with // sum greater than S int smallestSubarray(int K, int S, int arr[], int N) { // Store the first index of // the current subarray int start = 0; // Store the last index of // the the current subarray int end = 0; // Store the sum of the // current subarray int currSum = arr[0]; // Store the length of // the smallest subarray int res = INT_MAX; while (end < N - 1) { // If sum of the current subarray <= S // or length of current subarray <= K if (currSum <= S || (end - start + 1) <= K) { // Increase the subarray // sum and size currSum += arr[++end]; } // Otherwise else { // Update to store the minimum // size of subarray obtained res = min(res, end - start + 1); // Decrement current subarray // size by removing first element currSum -= arr[start++]; } } // Check if it is possible to reduce // the length of the current window while (start < N) { if (currSum > S && (end - start + 1) > K) res = min(res, (end - start + 1)); currSum -= arr[start++]; } return res; } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int K = 1, S = 8; int N = sizeof(arr) / sizeof(arr[0]); cout << smallestSubarray(K, S, arr, N); } |
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to find the length of the // smallest subarray of size > K with // sum greater than S public static int smallestSubarray(int K, int S, int[] arr, int N) { // Store the first index of // the current subarray int start = 0; // Store the last index of // the the current subarray int end = 0; // Store the sum of the // current subarray int currSum = arr[0]; // Store the length of // the smallest subarray int res = Integer.MAX_VALUE; while (end < N - 1) { // If sum of the current subarray <= S // or length of current subarray <= K if (currSum <= S || (end - start + 1) <= K) { // Increase the subarray // sum and size currSum += arr[++end]; } // Otherwise else { // Update to store the minimum // size of subarray obtained res = Math.min(res, end - start + 1); // Decrement current subarray // size by removing first element currSum -= arr[start++]; } } // Check if it is possible to reduce // the length of the current window while (start < N) { if (currSum > S && (end - start + 1) > K) res = Math.min(res, (end - start + 1)); currSum -= arr[start++]; } return res; } // Driver Code public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; int K = 1, S = 8; int N = arr.length; System.out.print(smallestSubarray(K, S, arr, N)); } } // This code is contributed by akhilsaini |
Python3
# Python3 program to implement # the above approach import sys # Function to find the length of the # smallest subarray of size > K with # sum greater than S def smallestSubarray(K, S, arr, N): # Store the first index of # the current subarray start = 0 # Store the last index of # the the current subarray end = 0 # Store the sum of the # current subarray currSum = arr[0] # Store the length of # the smallest subarray res = sys.maxsize while end < N - 1: # If sum of the current subarray <= S # or length of current subarray <= K if ((currSum <= S) or ((end - start + 1) <= K)): # Increase the subarray # sum and size end = end + 1; currSum += arr[end] # Otherwise else: # Update to store the minimum # size of subarray obtained res = min(res, end - start + 1) # Decrement current subarray # size by removing first element currSum -= arr[start] start = start + 1 # Check if it is possible to reduce # the length of the current window while start < N: if ((currSum > S) and ((end - start + 1) > K)): res = min(res, (end - start + 1)) currSum -= arr[start] start = start + 1 return res; # Driver Code if __name__ == "__main__": arr = [ 1, 2, 3, 4, 5 ] K = 1 S = 8 N = len(arr) print(smallestSubarray(K, S, arr, N)) # This code is contributed by akhilsaini |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the length of the // smallest subarray of size > K with // sum greater than S static int smallestSubarray(int K, int S, int[] arr, int N) { // Store the first index of // the current subarray int start = 0; // Store the last index of // the the current subarray int end = 0; // Store the sum of the // current subarray int currSum = arr[0]; // Store the length of // the smallest subarray int res = int.MaxValue; while (end < N - 1) { // If sum of the current subarray <= S // or length of current subarray <= K if (currSum <= S || (end - start + 1) <= K) { // Increase the subarray // sum and size currSum += arr[++end]; } // Otherwise else { // Update to store the minimum // size of subarray obtained res = Math.Min(res, end - start + 1); // Decrement current subarray // size by removing first element currSum -= arr[start++]; } } // Check if it is possible to reduce // the length of the current window while (start < N) { if (currSum > S && (end - start + 1) > K) res = Math.Min(res, (end - start + 1)); currSum -= arr[start++]; } return res; } // Driver Code static public void Main() { int[] arr = { 1, 2, 3, 4, 5 }; int K = 1, S = 8; int N = arr.Length; Console.Write(smallestSubarray(K, S, arr, N)); } } // This code is contributed by akhilsaini |
2
Time Complexity: O(N)
Auxiliary Space:O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Smallest subarray with sum greater than a given value
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Maximum subarray size, such that all subarrays of that size have sum less than k
- First subarray having sum at least half the maximum sum of any subarray of size K
- Smallest subarray such that all elements are greater than K
- Smallest subarray having an element with frequency greater than that of other elements
- Maximum sum subarray of size K with sum less than X
- Smallest subarray whose sum is multiple of array size
- Count the subarray with sum strictly greater than the sum of remaining elements
- Check if a subarray exists with sum greater than the given Array
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Highest and Smallest power of K less than and greater than equal to N respectively
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Maximum size of square such that all submatrices of that size have sum less than K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsaini

