Hi @Mark_Humeniuk
There is no issue with our given approach.
Let’s take few examples:
- We have an input array: [2, 1, 5, 2, 3, 2], S=7
The length of the smallest subarray with a sum greater than or equal to ‘7’ is 2 because we have a [5, 2] as a subarray of length 2.
- We have another input array: [3, 4, 1, 1, 6], S=8
The length of the smallest subarray with a sum greater than or equal to ‘8’ is 3 because we have two possible subarrays [3, 4, 1] or [1, 1, 6] of length 3.
- We have another input array: [2, 1, 5, 2, 8], S=7
The length of the smallest subarray with a sum greater than or equal to ‘7’ is 1 because we have one possible subarray [8] of length 1.
You can verify all the above-mentioned examples in the code as follows:
Secondly, there exist many approaches for solving any problem. Our given approach is just for implementation purposes. If you want to use any other approach which seems easier to you, then you can use it.
Hope it will help, Thank you