Smallest Subarray with Sum K from an Array - GeeksforGeeks

19 Apr, 2022
Given an array arr[] consisting of N integers, the task is to find the length of the Smallest subarray with a sum equal to K.
Examples:
Input: arr[] = {2, 4, 6, 10, 2, 1}, K = 12 Output: 2 Explanation: All possible subarrays with sum 12 are {2, 4, 6} and {10, 2}.
Input: arr[] = {-8, -8, -3, 8}, K = 5 Output: 2
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and for each subarray, check if its sum is equal to K or not. Print the minimum length of all such subarrays. 
Time Complexity: O(N2)Auxiliary Space: O(1)
Efficient approach: The above approach can be further optimized using the Prefix Sum technique and Map. 
Follow the steps below to solve the problem:
currPrefixSum will store the prefix sum ending at ith index.
Iterate over the array and keep calculating currPrefixSum.Check if, currPrefixSum is equal to K.If yes, then use this length of subarray (currPrefixSum) to minimize the result.Also, check if the required prefix sum (currPrefixSum – K) has been calculated previously or not.If the required prefix sum is calculated previously then find its last occurrence of that prefix sum and use it to calculate the length of the current prefix sum equal to K by (current index – last occurrence of required prefix sum) and use it to minimize the result.Store the currPrefixSum which is ending at ith index into the map.
Check if, currPrefixSum is equal to K.If yes, then use this length of subarray (currPrefixSum) to minimize the result.
If yes, then use this length of subarray (currPrefixSum) to minimize the result.
Also, check if the required prefix sum (currPrefixSum – K) has been calculated previously or not.If the required prefix sum is calculated previously then find its last occurrence of that prefix sum and use it to calculate the length of the current prefix sum equal to K by (current index – last occurrence of required prefix sum) and use it to minimize the result.
If the required prefix sum is calculated previously then find its last occurrence of that prefix sum and use it to calculate the length of the current prefix sum equal to K by (current index – last occurrence of required prefix sum) and use it to minimize the result.
Store the currPrefixSum which is ending at ith index into the map.
Finally, return the result. 
Below is the implementation of the above approach:
C++
Java
Python3
Javascript
// c++ code to implement the above idea#include <bits/stdc++.h>using namespace std;   // Function to find the Smallest Subarray with// Sum K from an Arrayint smallestSubarraySumK(vector<int> & arr, int k ){     // Use map here to store the prefixSum ending    // at ith index.    unordered_map<long long, int> unmap;    int n = arr.size();     // Store the current Prefix sum till ith index;    long long currPrefixSum = 0;     // Store the minimum size subarray whose sum is K    long long result = INT_MAX;     for(int i = 0; i < n; i++){        currPrefixSum += arr[i];         // Check if the current prefix sum is equals to K        if(currPrefixSum == k){            long long currLen = i + 1;            result = min(result, currLen);        }         // Required PrefixSum        long long requirePrefixSum            = currPrefixSum - k;         // Check if there exist any required        // Prefix Sum or not        if(unmap.count(requirePrefixSum)){            long long foundIdx =                unmap[requirePrefixSum];            long long currIdx = i;             result = min(result,                           currIdx - foundIdx);        }         // Store the current prefix sum ending        // at i        unmap[currPrefixSum] = i;    }     if(result >= INT_MAX) return -1;    // return the result    return result;}   // Driver codeint main(){         vector<int> arr = {-8, -8, -3, 8};    int k = 5;     cout << smallestSubarraySumK(arr, k);    return 0;}
// Java Program to implement// the above approachimport java.util.*;public class Main { // Function to find the Smallest Subarray with Sum// K from an Arraystatic int smallestSubarraySumK(int arr[], int n, int K){    // Use map here to store the prefixSum ending    // at ith index.    HashMap<Integer,    Integer> mp = new HashMap<Integer,    Integer>();     // Store the current Prefix sum till ith    // index;    int currPrefixSum = 0;     // Store the minimum size subarray whose    // sum is K    int result = Integer.MAX_VALUE;     for(int i = 0; i < n; i++){        currPrefixSum += arr[i];         // Check if the current prefix sum is        // equals to K        if(currPrefixSum == K){            int currLen = i + 1;            result = Math.min(result, currLen);        }         // Required PrefixSum        int requirePrefixSum = currPrefixSum - K;         // Check if there exist any required Prefix Sum or not        if(mp.containsKey(requirePrefixSum)){            int foundIdx = mp.get(requirePrefixSum);            int currIdx = i;             result = Math.min(result, currIdx                                - foundIdx);        }                // Store the current prefix sum ending at i        mp.put(currPrefixSum, i);    }     if(result >= Integer.MAX_VALUE) return -1;         // return the result    return result;} // Driver Codepublic static void main(String[] args){     int arr[] = {-8, -8, -3, 8};    int n = arr.length;    int K = 5;     System.out.println(smallestSubarraySumK(arr,                                              n, K));        }}
# Python3 program to implement# the above approachfrom collections import defaultdictimport sys # Function to find the length of the# smallest subarray with sum Kdef subArraylen(arr, n, K):     mp = defaultdict(lambda : 0)  currPrefixSum = 0  result = sys.maxsize  for i in range(n):    currPrefixSum += arr[i]    if(currPrefixSum == K):      currLen = i + 1      result = min(result, currLen)    requirePrefixSum = currPrefixSum - K           if(requirePrefixSum in mp.keys()):      foundIdx = mp[requirePrefixSum]      currIdx = i      result = min(result, currIdx - foundIdx)         mp[currPrefixSum] = i  return result           # Driver Codeif __name__ == "__main__":  arr = [-8, -8, -3, 8]  n = len(arr)     K = 5   ln = subArraylen(arr, n, K)   # Function call  if(ln == sys.maxsize):      print("-1")  else:      print(ln) # This code is contributed by Shivam Singh
<script> // JavaScript code to implement the above idea // Function to find the Smallest Subarray with// Sum K from an Arrayfunction smallestSubarraySumK(arr,k){     // Use map here to store the prefixSum ending    // at ith index.    let unmap = new Map();    let n = arr.length     // Store the current Prefix sum till ith index;    let currPrefixSum = 0;     // Store the minimum size subarray whose sum is K    let result = Number.MAX_VALUE;     for(let i = 0; i < n; i++)    {        currPrefixSum += arr[i];         // Check if the current prefix sum is equals to K        if(currPrefixSum == k){            let currLen = i + 1;            result = Math.min(result, currLen);        }         // Required PrefixSum        let requirePrefixSum            = currPrefixSum - k;         // Check if there exist any required        // Prefix Sum or not        if(unmap.has(requirePrefixSum)){            let foundIdx =                unmap.get(requirePrefixSum);            let currIdx = i;             result = Math.min(result,                           currIdx - foundIdx);        }         // Store the current prefix sum ending        // at i        unmap.set(currPrefixSum, i);    }     if(result >= Number.MAX_VALUE) return -1;         // return the result    return result;} // Driver codelet arr = [-8, -8, -3, 8];let k = 5; document.write(smallestSubarraySumK(arr, k)); // This code is contributed by shinjanpatra </script>
2
Time Complexity: O(N)Auxiliary Space: O(N)
SHIVAMSINGH67
Rohit_ranjan
vineetyadav24031999
rutvik_56
keenlearner13579
arorakashish0911
harendrakumar123
shinjanpatra
cpp-map
frequency-counting
Java-HashMap
prefix-sum
subarray
Arrays
Hash
Mathematical
Searching
prefix-sum
Arrays
Searching
Hash
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Internal Working of HashMap in Java
Count pairs with given sum
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Hashing | Set 2 (Separate Chaining)