educative.io

Why does initializing minLength as Infinity work, but 0 doesn't work?

Why does
let minLength = Infinity
work,

but
let minLength = 0
not work?

@Raghav_Mangrola It does not work with let minLength=0 because we are trying to find a subarray with minimum length, and the minimum length can be 1. When the minLength = Math.min(minLength, windowEnd - windowStart + 1) function is called with minLength=0, it will always return 0 because the minimum subarray we can find from our array is of size 1. When the minLength=Infinity, this means the Math.min(minLength, windowEnd - windowStart + 1) will always return the subarray size that we just found because our array has a finite length which will always be lesser than Infinity.

1 Like