Smallest Subarray with Sum K from an Array | C ++ Program to implement the above approach ; Function to find the length of the smallest subarray with sum K ; Stores the frequency of prefix sums in the array ; Initialize len as INT_MAX ; If sum of array till i - th index is less than K ; No possible subarray exists till i - th index ; Find the exceeded value ; If exceeded value is zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subArraylen ( int arr [ ] , int n , int K ) { unordered_map < int , int > mp ; mp [ arr [ 0 ] ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; mp [ arr [ i ] ] = i ; } int len = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < K ) continue ; else { int x = arr [ i ] - K ; if ( x == 0 ) len = min ( len , i ) ; if ( mp . find ( x ) == mp . end ( ) ) continue ; else { len = min ( len , i - mp [ x ] ) ; } } } return len ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 3 , 2 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 7 ; int len = subArraylen ( arr , n , K ) ; if ( len == INT_MAX ) { cout << " - 1" ; } else { cout << len << endl ; } return 0 ; }