arrays – Smallest subarray with sum equal to k


I want to find the length smallest subarray whose sum is equal to k.

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[] = { 1, 2, 4, 3, 2, 4, 1 }, K = 7
Output: 2