bad
of the same length as nums
with all zeros.nums[i]
.i - nums[i] + 1
to i
(modulo the array length).bad[i - nums[i] + 1]
and decrement bad[i + 1]
, handling wrap-around intervals appropriately.bad
to calculate the cumulative sum, which represents the number of overlapping 'bad' intervals for each rotation index.