educative.io

Educative

Why need Math.max on maxLength?

can replace the following statement:
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
with
maxLength = windowEnd - windowStart + 1;
?

1 Like

Hi, @Sid_Zhang,

Thank you for pointing this out, there is indeed no need for
maxLength = Math.max(maxLength, windowEnd - windowStart + 1)

This is because even when the left index of the sliding window is incremented by 1, the right index is incremented by 1 regardless due to the for loop. Due to this, the length of the sliding window can only increase or remain constant. It can never decrease. Hence, there will be no need for a max() function on maxLength.

We will correct this shortly.

Thank you for reaching out to us.