🧠
Sliding Window with Two Pointers
💡 This approach uses two pointers to maintain a window with at most k zeros, improving efficiency by avoiding repeated counting.
Intuition
Expand the right pointer to include elements, and move the left pointer to shrink the window when zeros exceed k, tracking the max window size.
Algorithm
- Initialize left pointer and zero count to 0.
- Expand right pointer through the array.
- Increment zero count when a zero is encountered.
- If zero count exceeds k, move left pointer right until zero count ≤ k.
- Update max length with the current window size.
💡 The key is dynamically adjusting the window size to maintain the zero count constraint efficiently.
def longestOnes(nums, k):
left = 0
zeros_count = 0
max_length = 0
for right in range(len(nums)):
if nums[right] == 0:
zeros_count += 1
while zeros_count > k:
if nums[left] == 0:
zeros_count -= 1
left += 1
max_length = max(max_length, right - left + 1)
return max_length
# Driver code
if __name__ == '__main__':
print(longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2)) # Output: 6
Line Notes
left = 0Initialize left pointer of the sliding window
zeros_count = 0Track number of zeros in current window
for right in range(len(nums))Expand right pointer to include new elements
if nums[right] == 0Increment zero count when zero encountered
while zeros_count > kShrink window from left if zero count exceeds k
if nums[left] == 0Decrement zero count when removing zero from window
max_length = max(max_length, right - left + 1)Update max length with current valid window size
public class Solution {
public static int longestOnes(int[] nums, int k) {
int left = 0, zerosCount = 0, maxLength = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) zerosCount++;
while (zerosCount > k) {
if (nums[left] == 0) zerosCount--;
left++;
}
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
System.out.println(longestOnes(nums, 2)); // Output: 6
}
}
Line Notes
int left = 0, zerosCount = 0, maxLength = 0;Initialize pointers and counters
for (int right = 0; right < nums.length; right++)Expand right pointer through array
if (nums[right] == 0) zerosCount++;Count zeros in current window
while (zerosCount > k)Shrink window from left if zero count exceeds k
if (nums[left] == 0) zerosCount--;Decrement zero count when removing zero
maxLength = Math.max(maxLength, right - left + 1);Update max length with current window size
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int longestOnes(vector<int>& nums, int k) {
int left = 0, zerosCount = 0, maxLength = 0;
for (int right = 0; right < (int)nums.size(); right++) {
if (nums[right] == 0) zerosCount++;
while (zerosCount > k) {
if (nums[left] == 0) zerosCount--;
left++;
}
maxLength = max(maxLength, right - left + 1);
}
return maxLength;
}
int main() {
vector<int> nums = {1,1,1,0,0,0,1,1,1,1,0};
cout << longestOnes(nums, 2) << endl; // Output: 6
return 0;
}
Line Notes
int left = 0, zerosCount = 0, maxLength = 0;Initialize sliding window pointers and zero count
for (int right = 0; right < (int)nums.size(); right++)Expand right pointer over array
if (nums[right] == 0) zerosCount++;Increment zero count when zero encountered
while (zerosCount > k)Shrink window from left if zero count exceeds k
if (nums[left] == 0) zerosCount--;Decrement zero count when removing zero from window
maxLength = max(maxLength, right - left + 1);Update max length with current window size
function longestOnes(nums, k) {
let left = 0, zerosCount = 0, maxLength = 0;
for (let right = 0; right < nums.length; right++) {
if (nums[right] === 0) zerosCount++;
while (zerosCount > k) {
if (nums[left] === 0) zerosCount--;
left++;
}
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
// Test
console.log(longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2)); // Output: 6
Line Notes
let left = 0, zerosCount = 0, maxLength = 0;Initialize pointers and zero count
for (let right = 0; right < nums.length; right++)Expand right pointer through array
if (nums[right] === 0) zerosCount++;Count zeros in current window
while (zerosCount > k)Shrink window from left if zero count exceeds k
if (nums[left] === 0) zerosCount--;Decrement zero count when removing zero
maxLength = Math.max(maxLength, right - left + 1);Update max length with current window size
Each element is visited at most twice by left and right pointers, leading to linear time.
💡 For n=100,000, this means about 200,000 operations, which is efficient for large inputs.
Interview Verdict: Accepted
This approach is optimal and efficient, suitable for large inputs and interview coding.