Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum consecutive 1s after n swaps in JavaScript
We are required to write a JavaScript function that takes in a binary array (array that contains only 0 or 1) as the first argument, and a number as the second argument representing the maximum number of swaps allowed.
We can change at most the specified number of 0s present in the array to 1s, and our function should return the length of the longest contiguous subarray that contains only 1s after making these changes.
Problem Statement
Given a binary array and a number of allowed swaps, find the maximum length of consecutive 1s we can achieve by flipping at most the given number of 0s to 1s.
For example, if the input is:
const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; const num = 2;
The expected output is 6, because after changing two zeros to 1s, we can get the last 6 elements as consecutive 1s.
Sliding Window Approach
This problem can be solved efficiently using the sliding window technique. We maintain a window that contains at most num zeros, and track the maximum window size encountered.
const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0];
const num = 2;
const longestOnes = (arr = [], num = 1) => {
let max = 0;
let left = 0;
let zeroCount = 0;
for (let right = 0; right < arr.length; right++) {
// Expand window by including current element
if (arr[right] === 0) {
zeroCount++;
}
// Shrink window from left if we exceed allowed zeros
while (zeroCount > num) {
if (arr[left] === 0) {
zeroCount--;
}
left++;
}
// Update maximum window size
max = Math.max(max, right - left + 1);
}
return max;
};
console.log(longestOnes(arr, num));
6
How It Works
The algorithm uses two pointers (left and right) to maintain a sliding window:
- Expand: Move the right pointer and include the current element in the window
- Count zeros: Keep track of zeros in the current window
- Shrink: If zeros exceed the allowed limit, move the left pointer until the window is valid
- Update: Track the maximum window size encountered
Step-by-Step Example
const arr = [1, 0, 1, 1, 0];
const num = 1;
const longestOnesWithSteps = (arr, num) => {
let max = 0;
let left = 0;
let zeroCount = 0;
console.log("Array:", arr);
console.log("Max swaps allowed:", num);
console.log("\nStep-by-step process:");
for (let right = 0; right < arr.length; right++) {
if (arr[right] === 0) {
zeroCount++;
}
while (zeroCount > num) {
if (arr[left] === 0) {
zeroCount--;
}
left++;
}
const windowSize = right - left + 1;
max = Math.max(max, windowSize);
console.log(`Window [${left}, ${right}]: ${arr.slice(left, right + 1)} - Size: ${windowSize}, Zeros: ${zeroCount}`);
}
return max;
};
console.log("\nResult:", longestOnesWithSteps(arr, num));
Array: [ 1, 0, 1, 1, 0 ] Max swaps allowed: 1 Step-by-step process: Window [0, 0]: [ 1 ] - Size: 1, Zeros: 0 Window [0, 1]: [ 1, 0 ] - Size: 2, Zeros: 1 Window [0, 2]: [ 1, 0, 1 ] - Size: 3, Zeros: 1 Window [0, 3]: [ 1, 0, 1, 1 ] - Size: 4, Zeros: 1 Window [2, 4]: [ 1, 1, 0 ] - Size: 3, Zeros: 1 Result: 4
Conclusion
The sliding window approach efficiently solves this problem in O(n) time complexity. By maintaining a valid window with at most the allowed number of zeros, we can find the maximum consecutive 1s achievable after swaps.
