educative.io

Educative

Solution doesn't work

The given solution doesn’t work for input: [-4,-3,-2,-1,0,1,2,3,4], sum = 3.
Output: 0
Expected Output: 1

Can anyone tell me what’s the actual working optimal solution? Or I am wrong?

For now, I am able to solve it by using sliding window pattern with increasing window size from 1 to len(array). But not sure if it is optimal, since my solution is O(N^2) (correct me if I am wrong please, Thanks!).

This is about “Smallest Subarray with a given sum” problem.