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
Consecutive ones with a twist in JavaScript
We are required to write a JavaScript function that takes in a binary array (an array that consists of only 0 and 1), arr, as the only argument. Our function should find the maximum number of consecutive 1s in this array if we can flip at most one 0.
Problem Statement
Given a binary array, find the maximum number of consecutive 1s we can achieve by flipping at most one 0 to 1.
For example, if the input array is:
[1, 0, 1, 1, 0]
Then the output should be:
4
Output Explanation
If we flip the 0 at index 1 in the array, we will get 4 consecutive 1s: [1, 1, 1, 1, 0].
Approach: Sliding Window Technique
We use a sliding window approach with two pointers to track a window that contains at most one 0. The algorithm expands the window by moving the right pointer and contracts it when we have more than one 0.
Implementation
const arr = [1, 0, 1, 1, 0];
const findMaximumOne = (nums = []) => {
let count = 0; // Count of zeros in current window
let first = -1; // Position of first zero in window
let i = 0, j = 0; // Left and right pointers
let res = -Infinity; // Maximum consecutive ones found
while (j < nums.length) {
if (nums[j] === 1) {
// If current element is 1, update result
res = Math.max(res, j - i + 1);
} else {
// If current element is 0
count++;
if (count == 2) {
// If we have 2 zeros, move left pointer past first zero
i = first + 1;
count--;
}
first = j; // Update position of current zero
}
j++; // Move right pointer
}
return res;
};
console.log(findMaximumOne(arr));
4
How It Works
The algorithm maintains a sliding window with at most one 0:
- Expand window: Move right pointer and include current element
- Track zeros: Count zeros in current window
- Contract window: When we have 2 zeros, move left pointer past the first zero
- Update result: Track maximum window size containing at most one 0
Alternative Approach: Simple Two-Pass
const findMaxOnesAlternative = (nums) => {
let maxLength = 0;
// Try flipping each 0 and find max consecutive 1s
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
nums[i] = 1; // Flip 0 to 1
// Find longest sequence of 1s
let currentLength = 0;
let maxCurrent = 0;
for (let j = 0; j < nums.length; j++) {
if (nums[j] === 1) {
currentLength++;
maxCurrent = Math.max(maxCurrent, currentLength);
} else {
currentLength = 0;
}
}
maxLength = Math.max(maxLength, maxCurrent);
nums[i] = 0; // Revert back
}
}
return maxLength;
};
const testArray = [1, 0, 1, 1, 0];
console.log(findMaxOnesAlternative(testArray));
4
Conclusion
The sliding window approach efficiently solves this problem in O(n) time complexity. It maintains at most one 0 in the current window and tracks the maximum window size, giving us the answer for consecutive 1s after flipping one 0.
