Consecutive Ones III

HardAcc. 81.4%
+45 XP 20

Optimized Flipping

Given a binary array and an integer k, find the longest subarray containing only 1s after flipping at most k 0s.

The Assignment

Your function receives nums and k.

  1. Sliding window: expand right.
  2. If you hit a 0, decrement k.
  3. If k < 0, move left forward and if you move past a 0, increment k.
  4. Print the maximum window size.

01EXAMPLE 1

Inputnums=[1,1,1,0,0,0,1,1,1,1,0], k=2
Output6

Explanation: Flipping two 0s at idx 5, 10 allows a streak of 6.

Constraints

  • Single pass (O(n)).
ArraysSliding Window
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,1,1,0,0,0,1,1,1,1,0]
k2

Expected Output

6

Click RUN to test your solution