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 smallest possible subarray satisfying the required condition is the entire array.

Therefore, the length is 3.

Input:A[] = {2, 1, 1, -4, 3, 1, -1, 2}, K = 5

Output:4

**Naive Approach:**

The simplest approach to solve the problem is to generate all possible subarrays of the given array and check which subarray sum is greater than or equal to **K**. Among all such subarrays satisfying the condition, print the subarray having minimum length.

**Time Complexity:**O(N^{2})

**Auxiliary Space:** O(1)

**Efficient Approach:**

The above approach can be further optimized using Prefix Sum Array and Binary search. Follow the steps below:

- Initialize an array to store the
**Prefix sum**of the original array. - Hash the prefix sum array with the indices using a Map.
- If a
*greater sum with a lesser index*is already found, then there is no point of hashing a prefix sum which is smaller than the largest prefix sum obtained till now. Therefore, hash the*increasing order*of prefix sum. - Traversing the array and if any element is greater than or equal to
**K**, return 1 as the answer. - Otherwise, for every element, perform
**Binary Search**over the indices**(i, n-1)**in the**prefix sum array**to find the first index with sum at least**K**. - Return the minimum length subarray obtained from the above steps.

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 perform Binary Search ` `// and return the smallest index with ` `// sum greater than value ` `int` `binary_search(map<` `int` `, vector<` `int` `> >& m, ` ` ` `int` `value, ` `int` `index) ` `{ ` ` ` `// Search the value in map ` ` ` `auto` `it = m.lower_bound(value); ` ` ` ` ` `// If all keys in the map ` ` ` `// are less then value ` ` ` `if` `(it == m.end()) ` ` ` `return` `0; ` ` ` ` ` `// Check if the sum is found ` ` ` `// at a greater index ` ` ` `auto` `it1 ` ` ` `= lower_bound(it->second.begin(), ` ` ` `it->second.end(), index); ` ` ` ` ` `if` `((it1 - it->second.begin()) ` ` ` `!= it->second.size()) ` ` ` `return` `*it1; ` ` ` ` ` `return` `0; ` `} ` ` ` `// Function to find the smallest subarray ` `// with sum greater than equal to K ` `int` `findSubarray(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Prefix sum array ` ` ` `int` `pre_array[n]; ` ` ` ` ` `// Stores the hashes to prefix sum ` ` ` `map<` `int` `, vector<` `int` `> > m; ` ` ` ` ` `pre_array[0] = arr[0]; ` ` ` `m[pre_array[0]].push_back(0); ` ` ` ` ` `// If any array element is ` ` ` `// greater than equal to k ` ` ` `if` `(arr[0] >= k) ` ` ` `return` `1; ` ` ` ` ` `int` `ans = INT_MAX; ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `pre_array[i] ` ` ` `= arr[i] + pre_array[i - 1]; ` ` ` ` ` `// If prefix sum exceeds K ` ` ` `if` `(pre_array[i] >= k) ` ` ` ` ` `// Update size of subarray ` ` ` `ans = min(ans, i + 1); ` ` ` ` ` `auto` `it = m.rbegin(); ` ` ` ` ` `// Hash prefix sum in ` ` ` `// increasing order ` ` ` `if` `(pre_array[i] >= it->first) ` ` ` `m[pre_array[i]].push_back(i); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `int` `temp ` ` ` `= binary_search(m, ` ` ` `pre_array[i - 1] + k, ` ` ` `i); ` ` ` `if` `(temp == 0) ` ` ` `continue` `; ` ` ` ` ` `// Update size of subarray ` ` ` `ans = min(ans, temp - i + 1); ` ` ` `} ` ` ` ` ` `// If any subarray is found ` ` ` `if` `(ans <= n) ` ` ` `return` `ans; ` ` ` ` ` `// If no such subarray exists ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 1, -4, 3, 1, -1, 2 }; ` ` ` ` ` `int` `k = 5; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << findSubarray(arr, n, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:*** O(NlogN)*

**Auxiliary Space:*** O(N)*

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:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- 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
- 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
- Longest Subarray with Sum greater than Equal to Zero
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest number greater than Y with sum of digits equal to X
- Check if a subarray exists with sum greater than the given Array
- Smallest Special Prime which is greater than or equal to a given number
- Smallest subarray such that all elements are greater than K
- Smallest subarray having an element with frequency greater than that of other elements
- Count the subarray with sum strictly greater than the sum of remaining elements
- Longest subarray having average greater than or equal to x
- Longest Subarray with first element greater than or equal to Last element
- Longest subarray having average greater than or equal to x | Set-2
- Subarray with difference between maximum and minimum element greater than or equal to its length

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.