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.
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
Example 1:
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.0 <= k <= nums.lengthProblem summary: Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Sliding Window
[1,1,1,0,0,0,1,1,1,1,0] 2
[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1] 3
longest-substring-with-at-most-k-distinct-characters)longest-repeating-character-replacement)max-consecutive-ones)max-consecutive-ones-ii)longest-subarray-of-1s-after-deleting-one-element)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1004: Max Consecutive Ones III
class Solution {
public int longestOnes(int[] nums, int k) {
int l = 0, cnt = 0;
for (int x : nums) {
cnt += x ^ 1;
if (cnt > k) {
cnt -= nums[l++] ^ 1;
}
}
return nums.length - l;
}
}
// Accepted solution for LeetCode #1004: Max Consecutive Ones III
func longestOnes(nums []int, k int) int {
l, cnt := 0, 0
for _, x := range nums {
cnt += x ^ 1
if cnt > k {
cnt -= nums[l] ^ 1
l++
}
}
return len(nums) - l
}
# Accepted solution for LeetCode #1004: Max Consecutive Ones III
class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
l = cnt = 0
for x in nums:
cnt += x ^ 1
if cnt > k:
cnt -= nums[l] ^ 1
l += 1
return len(nums) - l
// Accepted solution for LeetCode #1004: Max Consecutive Ones III
struct Solution;
impl Solution {
fn longest_ones(a: Vec<i32>, k: i32) -> i32 {
let n = a.len();
let mut sum = 0;
let mut res = 0;
let mut start = 0;
let mut end = 0;
while end < n {
if sum <= k {
if a[end] == 0 {
sum += 1;
}
end += 1;
} else {
if a[start] == 0 {
sum -= 1;
}
start += 1;
}
if sum <= k {
res = res.max(end - start);
}
}
res as i32
}
}
#[test]
fn test() {
let a = vec![1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0];
let k = 2;
let res = 6;
assert_eq!(Solution::longest_ones(a, k), res);
let a = vec![0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1];
let k = 3;
let res = 10;
assert_eq!(Solution::longest_ones(a, k), res);
let a = vec![0, 0, 0, 1];
let k = 4;
let res = 4;
assert_eq!(Solution::longest_ones(a, k), res);
}
// Accepted solution for LeetCode #1004: Max Consecutive Ones III
function longestOnes(nums: number[], k: number): number {
let [l, cnt] = [0, 0];
for (const x of nums) {
cnt += x ^ 1;
if (cnt > k) {
cnt -= nums[l++] ^ 1;
}
}
return nums.length - l;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
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.