We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

@shubhanka Using Arrays.stream method he sends a Array 's', starting index 'i' end index 'i+m' as range which returns a set of integers from Array 's' for that specified range then using sum() method he checks the sum to equals 'd', if conditon satifies it increase the total plus one.

the index range is inclusive of the start and end indices: [0, 1, 2].

we are looking at the set of chocolates from i to i+m and determining if that set meets our criteria. if it does, we increment total. then we increment i and look at the next set, and so on until we have traversed the array.

i like victorz's analogy below: it is a "sliding window."

## Birthday Chocolate

You are viewing a single comment's thread. Return to all comments →

Please explain the logic behind it.

@shubhanka Using Arrays.stream method he sends a Array 's', starting index 'i' end index 'i+m' as range which returns a set of integers from Array 's' for that specified range then using sum() method he checks the sum to equals 'd', if conditon satifies it increase the total plus one.

*

starting index is i=0 and end index is i+m=2 so the 2 index is excluded?? *the index range is inclusive of the start and end indices: [0, 1, 2].

we are looking at the set of chocolates from i to i+m and determining if that set meets our criteria. if it does, we increment total.

then we increment i and look at the next set, and so on until we have traversed the array.i like victorz's analogy below: it is a "sliding window."

i hope that helps.

For the same program, how can we get the result if the required chocolate squares are not consecutive?

thats not what the problem states ( these types of problem are called maximum-sub array problems ) ,

Still you want to solve this problem, you need to use Combinations, which will increase the diffuculty.

what is the i < n-m?

Yes the 2 index is excluded.

Thank you