Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a binary array nums.
You can do the following operation on the array any number of times (possibly zero):
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. If it is impossible, return -1.
Example 1:
Input: nums = [0,1,1,1,0,0]
Output: 3
Explanation:
We can do the following operations:
nums = [1,0,0,1,0,0].nums = [1,1,1,0,0,0].nums = [1,1,1,1,1,1].Example 2:
Input: nums = [0,1,1,1]
Output: -1
Explanation:
It is impossible to make all elements equal to 1.
Constraints:
3 <= nums.length <= 1050 <= nums[i] <= 1Problem summary: You are given a binary array nums. You can do the following operation on the array any number of times (possibly zero): Choose any 3 consecutive elements from the array and flip all of them. 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. If it is impossible, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation · Sliding Window
[0,1,1,1,0,0]
[0,1,1,1]
minimum-number-of-k-consecutive-bit-flips)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3191: Minimum Operations to Make Binary Array Elements Equal to One I
class Solution {
public int minOperations(int[] nums) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
if (i + 2 >= n) {
return -1;
}
nums[i + 1] ^= 1;
nums[i + 2] ^= 1;
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3191: Minimum Operations to Make Binary Array Elements Equal to One I
func minOperations(nums []int) (ans int) {
for i, x := range nums {
if x == 0 {
if i+2 >= len(nums) {
return -1
}
nums[i+1] ^= 1
nums[i+2] ^= 1
ans++
}
}
return
}
# Accepted solution for LeetCode #3191: Minimum Operations to Make Binary Array Elements Equal to One I
class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
for i, x in enumerate(nums):
if x == 0:
if i + 2 >= len(nums):
return -1
nums[i + 1] ^= 1
nums[i + 2] ^= 1
ans += 1
return ans
// Accepted solution for LeetCode #3191: Minimum Operations to Make Binary Array Elements Equal to One I
/**
* [3191] Minimum Operations to Make Binary Array Elements Equal to One I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_operations(nums: Vec<i32>) -> i32 {
let mut result = 0;
let mut nums = nums;
let mut pos = 0;
while pos < nums.len() - 3 {
if nums[pos] == 1 {
pos += 1;
continue;
}
nums[pos] = 1;
Self::reverse(pos + 1, &mut nums);
Self::reverse(pos + 2, &mut nums);
result += 1;
pos += 1;
}
if nums[pos] ^ nums[pos + 1] == 0 && nums[pos + 1] ^ nums[pos + 2] == 0 {
if nums[pos] == 1 {
result
} else {
result + 1
}
} else {
-1
}
}
fn reverse(pos: usize, nums: &mut Vec<i32>) {
nums[pos] = if nums[pos] == 1 { 0 } else { 1 }
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3191() {
assert_eq!(-1, Solution::min_operations(vec![0, 1, 1, 0, 1, 0, 0]));
assert_eq!(3, Solution::min_operations(vec![0, 1, 1, 1, 0, 0]));
assert_eq!(-1, Solution::min_operations(vec![0, 1, 1, 1]));
}
}
// Accepted solution for LeetCode #3191: Minimum Operations to Make Binary Array Elements Equal to One I
function minOperations(nums: number[]): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
if (nums[i] === 0) {
if (i + 2 >= n) {
return -1;
}
nums[i + 1] ^= 1;
nums[i + 2] ^= 1;
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.