Minimum Operations to Make Binary Array Elements Equal to One I (LeetCode 3191): Solution and Explanation

Medium
Array
Bit manipulation
Queue
Sliding window
Prefix sum

Slide a 3-wide flip window from left to right. Whenever the cell at the window's left edge is 0, flipping right there is the only way left to fix it, so the greedy count is both forced and minimal. If the last two cells end as 0, the answer is -1.

Intuition

Two facts simplify the operation. Flipping the same three cells twice undoes itself, and the order of flips does not change the result. So the only question is which start indices are used, each at most once.

Look at index 0. The only flip that touches it is the one starting at 0. If nums[0] is 0 that flip is mandatory; if it is 1 that flip is forbidden. Either way the choice is forced.

After deciding index 0, the same argument applies to index 1, then 2, and so on. The scan never has a real choice, so the number of flips it makes is the minimum. The last two indices cannot start a flip, so if either is still 0 nothing can fix it.

Approach: Greedy forced flips

  1. Set ops = 0.
  2. For i from 0 to n - 3, if nums[i] == 0, flip nums[i], nums[i + 1] and nums[i + 2], and add 1 to ops.
  3. After the loop, if nums[n - 2] or nums[n - 1] is 0, return -1.
  4. Otherwise return ops.

Step-by-step example

nums = [0,1,1,1,0,0]
  1. i = 0: nums[0] = 0, flip 0..2. Array becomes [1,0,0,1,0,0], ops = 1.
  2. i = 1: nums[1] = 0, flip 1..3. Array becomes [1,1,1,0,0,0], ops = 2.
  3. i = 2: nums[2] = 1, skip.
  4. i = 3: nums[3] = 0, flip 3..5. Array becomes [1,1,1,1,1,1], ops = 3.
  5. The last two cells are 1, so the answer is 3.

Code: Python, Java, C++

1class Solution:
2    def minOperations(self, nums: List[int]) -> int:
3        n = len(nums)
4        ops = 0
5        for i in range(n - 2):
6            if nums[i] == 0:
7                nums[i] ^= 1
8                nums[i + 1] ^= 1
9                nums[i + 2] ^= 1
10                ops += 1
11        if nums[n - 2] == 0 or nums[n - 1] == 0:
12            return -1
13        return ops
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index is visited once and each flip touches three cells.

Space

O(1)

The array is flipped in place; only the counter is extra.

Other approaches

Lazy flip count with a queue (time O(n), space O(1))

Instead of writing the flips into the array, keep how many active flips cover the current index (expiring after 3 steps). This leaves the input untouched and extends to any window size k, as in Minimum Number of K Consecutive Bit Flips.

Breadth-first search over states (time O(2^n * n), space O(2^n))

Treat each array as a state and each flip as an edge. Correct in principle but exponential in n, so only useful to confirm the greedy on tiny inputs.

Common mistakes

  • Looping all the way to n - 1, which flips past the end of the array.
  • Forgetting the final check on the last two cells and returning ops for an impossible input.
  • Trying to be clever about which zero to fix first. The leftmost zero has exactly one fix, so there is nothing to choose.

Frequently asked questions

Why is the greedy count the minimum?

Each start index is used at most once, and the scan shows that whether index i is used is fully determined by the cells before it. There is exactly one valid set of flips, so its size is the minimum.

When is the answer -1?

When the forced flips leave a 0 in one of the last two cells. No flip can start there, and any flip that reaches them would also change cells that are already fixed.

Is it fine to modify nums?

Yes for this problem. If you need to keep the input, track active flips with a small counter instead, as in the lazy queue approach.

What is the time and space complexity?

O(n) time and O(1) extra space.