educative.io

Educative

Why loop until arr.length - 2?

Why in the searchTriplets method for-loop do they stop at 2 before the end of the array?

for (int i = 0; i < arr.length - 2; i++) {

Is it so we can get rid of the remaining numbers if there are not enough left to make a triplet?

Hi,

Yes, that’s the reason.
For the last pair, the current number should have at least 2 numbers on right to make a triplet.

Thanks