Minimum Operations to Make Binary Array Elements Equal to One II
MediumUpdated: Aug 2, 2025
Practice on:
Problem
You are given a binary array nums.
You can do the following operation on the array any number of times (possibly zero):
- Choose any index
ifrom the array and flip all the elements from indexito the end of the array.
Flipping an element means changing its value from 0 to 1, and from 1 to 0.
Return the minimum number of operations required to make all elements in
nums equal to 1.
Examples
Example 1:
Input: nums = [0,1,1,0,1]
Output: 4
Explanation:
We can do the following operations:
* Choose the index `i = 1`. The resulting array will be `nums = [0,_**0**_ ,_**0**_ ,_**1**_ ,_**0**_]`.
* Choose the index `i = 0`. The resulting array will be `nums = [_**1**_ ,_**1**_ ,_**1**_ ,_**0**_ ,_**1**_]`.
* Choose the index `i = 4`. The resulting array will be `nums = [1,1,1,0,_**0**_]`.
* Choose the index `i = 3`. The resulting array will be `nums = [1,1,1,_**1**_ ,_**1**_]`.
Example 2:
Input: nums = [1,0,0,0]
Output: 1
Explanation:
We can do the following operation:
* Choose the index `i = 1`. The resulting array will be `nums = [1,_**1**_ ,_**1**_ ,_**1**_]`.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1
Solution
Method 1 – Flip State Tracking (Greedy)
Intuition
The algorithm minimizes operations by toggling elements only when necessary, efficiently moving towards making all elements 1. By tracking the current flip state, we avoid redundant operations and only flip when the current value matches the flip state.
Approach
- Initialize a variable to track the number of flips and the current flip state.
- Iterate through the array from left to right.
- For each element, if its value equals the current flip state, increment the flip count and toggle the flip state.
- Continue until all elements are processed.
- Return the total number of flips.
Code
Java
class Solution {
public int minOperations(int[] nums) {
int flips = 0;
int flipped = 0;
for (int num : nums) {
if (num == flipped) {
flips++;
flipped ^= 1;
}
}
return flips;
}
}
C++
class Solution {
public:
int minOperations(vector<int>& nums) {
int flips = 0, flipped = 0;
for (int num : nums) {
if (num == flipped) {
flips++;
flipped ^= 1;
}
}
return flips;
}
};
Go
func minOperations(nums []int) int {
flips, flipped := 0, 0
for _, num := range nums {
if num == flipped {
flips++
flipped ^= 1
}
}
return flips
}
Python
class Solution:
def minOperations(self, nums: list[int]) -> int:
flips = 0
flipped = 0
for num in nums:
if num == flipped:
flips += 1
flipped ^= 1
return flips
Rust
impl Solution {
pub fn min_operations(nums: Vec<i32>) -> i32 {
let mut flips = 0;
let mut flipped = 0;
for num in nums {
if num == flipped {
flips += 1;
flipped ^= 1;
}
}
flips
}
}
TypeScript
class Solution {
minOperations(nums: number[]): number {
let flips = 0, flipped = 0;
for (const num of nums) {
if (num === flipped) {
flips++;
flipped ^= 1;
}
}
return flips;
}
}
Complexity
- ⏰ Time complexity:
O(n), since each element is processed once. - 🧺 Space complexity:
O(1), only a few variables are used.