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
Naive Approach
The idea is to find all subarrays and in that choose those subarrays whose sum is greater than S and whose length is greater than K. After that from those subarrays, choose that subarray whose length is minimum. Then print the length of that subarray.
Steps that were to follow the above approach:
- Make a variable “ans” and initialize it with the maximum value
- After that run two nested loops to find all subarrays
- While finding all subarray calculate their size and sum of all elements of that subarray
- If the sum of all elements is greater than S and its size is greater than K, then update answer with minimum of answer and length of the subarray
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestSubarray( int K, int S, int arr[], int N)
{
int ans=INT_MAX;
for ( int i=0;i<N;i++){
int size=0;
int sum=0;
for ( int j=i;j<N;j++){
size++;
sum+=arr[j];
if (size>K && sum>S){
if (size<ans){ans=size;}
}
}
}
return ans;
}
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
import java.util.*;
public class Main
{
static int smallestSubarray( int K, int S, int [] arr, int N)
{
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int size = 0 ;
int sum = 0 ;
for ( int j = i; j < N; j++) {
size++;
sum += arr[j];
if (size > K && sum > S) {
if (size < ans) {
ans = size;
}
}
}
}
return ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int K = 1 , S = 8 ;
int N = arr.length;
System.out.println(smallestSubarray(K, S, arr, N));
}
}
|
Python3
def smallestSubarray(K, S, arr, N):
ans = float ( 'inf' )
for i in range (N):
size = 0
sum = 0
for j in range (i, N):
size + = 1
sum + = arr[j]
if size > K and sum > S:
if size < ans:
ans = size
return ans
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 1
S = 8
N = len (arr)
print (smallestSubarray(K, S, arr, N))
|
C#
using System;
class GFG {
static int SmallestSubarray( int K, int S, int [] arr,
int N)
{
int ans = int .MaxValue;
for ( int i = 0; i < N; i++) {
int size = 0;
int sum = 0;
for ( int j = i; j < N; j++) {
size++;
sum += arr[j];
if (size > K && sum > S) {
if (size < ans) {
ans = size;
}
}
}
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int K = 1, S = 8;
int N = arr.Length;
Console.WriteLine(SmallestSubarray(K, S, arr, N));
}
}
|
Javascript
function smallestSubarray(K, S, arr, N) {
let ans = Infinity;
for (let i = 0; i < N; i++) {
let size = 0;
let sum = 0;
for (let j = i; j < N; j++) {
size += 1;
sum += arr[j];
if (size > K && sum > S) {
if (size < ans) {
ans = size;
}
}
}
}
return ans;
}
let arr = [1, 2, 3, 4, 5];
let K = 1;
let S = 8;
let N = arr.length;
console.log(smallestSubarray(K, S, arr, N));
|
Output-
2
Time Complexity: O(N2), because of two nested for loops
Auxiliary Space:O(1) , because no extra space has been used
Approach: The problem can be solved using Sliding Window Technique. Follow the steps below to solve the problem:
- Initialize two variables say, i = 0 and j = 0 both pointing to the start of array i.e index 0.
- Initialize a variable sum to store the sum of the subArray currently being processed.
- Traverse the array, arr[] and by incrementing j and adding arr[j]
- Take our the window length or the length of the current subArray which is given by j-i+1 (+1 because the indexes start from zero) .
- Firstly, check if the size of the current subArray i.e winLen here is greater than K. if this is not the case increment the j value and continue the loop.
- Else , we get that the size of the current subArray is greater than K, now we have to check if we meet the second condition i.e sum of the current Subarray is greater than S.
- If this is the case, we update minLength variable which stores the minimum length of the subArray satisfying the above conditions.
- At this time , we check if the size of the subArray can be reduced (by incrementing i ) such that it still is greater than K and sum is also greater than S. We constantly remove the ith element of the array from the sum to reduce the subArray size in the While loop and then increment j such that we move to the next element in the array .the
- Finally, print the minimum length of required subarray obtained that satisfies the conditions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestSubarray( int K, int S, int arr[], int N)
{
int start = 0;
int end = 0;
int currSum = arr[0];
int res = INT_MAX;
while (end < N - 1) {
if (currSum <= S || (end - start + 1) <= K) {
currSum += arr[++end];
}
else {
res = min(res, end - start + 1);
currSum -= arr[start++];
}
}
while (start < N) {
if (currSum > S && (end - start + 1) > K)
res = min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
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);
}
|
C
#include <limits.h>
#include <stdio.h>
int min( int num1, int num2)
{
return (num1 > num2) ? num2 : num1;
}
int smallestSubarray( int K, int S, int arr[], int N)
{
int start = 0;
int end = 0;
int currSum = arr[0];
int res = INT_MAX;
while (end < N - 1) {
if (currSum <= S || (end - start + 1) <= K) {
currSum += arr[++end];
}
else {
res = min(res, end - start + 1);
currSum -= arr[start++];
}
}
while (start < N) {
if (currSum > S && (end - start + 1) > K)
res = min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 1, S = 8;
int N = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" , smallestSubarray(K, S, arr, N));
}
|
Java
import java.io.*;
class GFG{
public static int smallestSubarray( int k, int s,
int [] array, int N)
{
int i= 0 ;
int j= 0 ;
int minLen = Integer.MAX_VALUE;
int sum = 0 ;
while (j < N)
{
sum += array[j];
int winLen = j-i+ 1 ;
if (winLen <= k)
j++;
else {
if (sum > s)
{
minLen = Math.min(minLen,winLen);
while (sum > s)
{
sum -= array[i];
i++;
}
j++;
}
}
}
return minLen;
}
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));
}
}
|
Python3
import sys
def smallestSubarray(K, S, arr, N):
start = 0
end = 0
currSum = arr[ 0 ]
res = sys.maxsize
while end < N - 1 :
if ((currSum < = S) or
((end - start + 1 ) < = K)):
end = end + 1 ;
currSum + = arr[end]
else :
res = min (res, end - start + 1 )
currSum - = arr[start]
start = start + 1
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;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 1
S = 8
N = len (arr)
print (smallestSubarray(K, S, arr, N))
|
C#
using System;
class GFG{
static int smallestSubarray( int K, int S,
int [] arr, int N)
{
int start = 0;
int end = 0;
int currSum = arr[0];
int res = int .MaxValue;
while (end < N - 1)
{
if (currSum <= S ||
(end - start + 1) <= K)
{
currSum += arr[++end];
}
else
{
res = Math.Min(res, end - start + 1);
currSum -= arr[start++];
}
}
while (start < N)
{
if (currSum > S && (end - start + 1) > K)
res = Math.Min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
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));
}
}
|
Javascript
<script>
function smallestSubarray(K, S, arr, N)
{
let start = 0;
let end = 0;
let currSum = arr[0];
let res = Number.MAX_SAFE_INTEGER;
while (end < N - 1)
{
if (currSum <= S
|| (end - start + 1) <= K)
{
currSum += arr[++end];
}
else {
res = Math.min(res, end - start + 1);
currSum -= arr[start++];
}
}
while (start < N)
{
if (currSum > S
&& (end - start + 1) > K)
res = Math.min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
let arr = [ 1, 2, 3, 4, 5 ];
let K = 1, S = 8;
let N = arr.length;
document.write(smallestSubarray(K, S, arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space:O(1)