Aug 2, 2024
[0, 1, 0, 1, 1], we need to swap elements to make ones contiguous.[0, 1] to get [1, 1, 1, 0, 0] (1 swap).Initial Thoughts:
length of window - count of ones = number of swaps needed.Counter Example:
[1, 0, 1, 0, 1, 1] with holes can lead to overestimation of swaps.New Approach:
k) that contains the maximum number of ones.[1, 0, 1, 0, 1, 1] with 4 ones, a window of size 4 can help determine how many zeros need to be swapped.k - max number of ones in the window.2*n (where n is the original array length), using modulo to avoid out-of-bounds errors.window size = right - left + 1