educative.io

Bug Report: Sliding Window - Smallest Subarray with a given sum

For one of the test cases: [2, 1, 5, 2, 8]; s=7, it says that the expected output is 1. The answer should be 2. The provided solution is wrong. There should be an if statement to check if the current window sum is equal to the expected sum, before re-calculating the minimum length. Then the algorithm would be correct.

Hi @Adam_Jones,

The correct answer is actually ‘1’. Probably you are missing the “smallest subarray” part? There are subarrays having a sum greater than or equal to ‘7’, but all of them are of size greater than ‘1’; here we are interested only in the smallest of such subarrays.

Let us know if this answers your question.

Actually, I did not notice the “greater than or equal to” part. I thought it was “equal to”. So it wants that smallest subarray that is greater than or equal to 7, not exactly equal to 7. Thanks for your help.