Imagine you have a binary array filled with 0s and 1s, and you're given a special power: you can flip at most k zeros into ones. Your mission is to find the maximum number of consecutive 1s you can create using this power strategically.
Given a binary array nums and an integer k, return the length of the longest subarray containing only 1s after flipping at most k zeros.
Example: If you have [1,1,1,0,0,0,1,1,1,1,0] and k = 2, you could flip the zeros at positions 3 and 4 to get [1,1,1,1,1,0,1,1,1,1,0], creating 5 consecutive ones. But there's an even better strategy!
Input & Output
Visualization
Time & Space Complexity
Binary search runs in O(log n) iterations, each checking O(n) subarrays
Only using variables for binary search bounds and counting
Constraints
- 1 โค nums.length โค 105
- nums[i] is either 0 or 1
- 0 โค k โค nums.length
- Follow up: Can you solve this in O(n) time and O(1) space?