Longest Subarray of 1s After Deleting One Element
Delete one element from an array of 0s and 1s and find the longest run of 1s that is left.
Given an array nums
Example 1
Output: 3
Explanation: delete the 0 to get [1, 1, 1].
Example 2
Output: 5
Explanation: delete the 0 at index 4 to join [1, 1, 1] and [1, 1].
Example 3
Output: 2
Explanation: one element must be deleted, even if it is a 1.
The Idea
Picture a row of lights, some on and some off. A 1 is a light that is on, a 0 one that is off. You must take exactly one light out of the row, and the lights on either side close the gap. How long a run of lit lights can you end up with?
Take Example 2, [0, 1, 1, 1, 0, 1, 1, 0, 1]
Which element should go? Deleting the 0 at index 4 joins the run of three and the run of two into five 1s. Deleting the 0 at index 7 joins two and one into only three. Deleting a 1 never helps when a 0 could go instead. Example 3 has no 0 at all, so a 1 has to go, and three 1s become two.
Trying every element as the one to delete works, but it repeats a lot of scanning. Is there a way to see the answer directly? Look at where the five 1s of the best deletion came from: indexes 1 to 6 of the original array, a stretch that held exactly one 0. Look at the picture below.
The orange box marks that stretch. Such a stretch, with a leftright
Any stretch that holds at most one 0 can be turned into a run of 1s with one deletion. If it holds one 0, delete the 0. If it holds none, delete a 1 at one end. Either way, the rest are all 1s, one fewer than the stretch had. So the question becomes: what is the longest stretch with at most one 0? The answer is its length minus 1.
Finding that stretch is a job for the sliding window technique: the rightleft
Solution Approach
Turning that idea into code means settling three things: how the window grows and shrinks, how to count the 0s inside it, and which length to report.
Growing and shrinking the window
Why not simply try deleting each element in turn? Each of the n choices needs a scan of the rest for the longest run of 1s, so that is about n² steps. For 10,000 elements, it is 100 million steps.
The window needs only one pass. rightleft
Is it safe that leftright
Counting the 0s in the window
How does the code know when the window holds two 0s? A counter, zerosrightleftzeros
The length to report
A window from leftrightright - left + 1right - left[1, 1, 1]
Reporting right - left + 1[1, 1, 0, 1][1, 1, 1]
Java Solution
The method longestSubarray
- First, set up the window:
starts at 0,left counts the 0s inside the window and starts at 0, andzeros holds the longest run found so far, also 0.best - A
loop movesfor over the array, one index at a time. Whenright is 0, add one tonums[right] .zeros - Inside it, a
loop runs whilewhile is more than 1. Each time, ifzeros is 0, subtract one fromnums[left] , then movezeros one step forward.left - Now the window holds at most one 0. Compare
withright - left and keep the larger.best - After
has passed the last element, returnright , the longest run of 1s any window left behind.best
The main[1, 0, 0, 1, 1]
public class LongestSubarrayOf1sAfterDeletingOneElement {
public static int longestSubarray(int[] nums) {
int left = 0;
int zeros = 0; // zeros in nums[left..right]
int best = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) {
zeros++;
}
while (zeros > 1) { // two zeros: shrink from the left
if (nums[left] == 0) {
zeros--;
}
left++;
}
best = Math.max(best, right - left); // window size minus the deleted element
}
return best;
}
public static void main(String[] args) {
System.out.println(longestSubarray(new int[] {1, 1, 0, 1}));
System.out.println(longestSubarray(new int[] {0, 1, 1, 1, 0, 1, 1, 0, 1}));
System.out.println(longestSubarray(new int[] {1, 1, 1}));
System.out.println(longestSubarray(new int[] {0, 0, 0}));
System.out.println(longestSubarray(new int[] {1}));
System.out.println(longestSubarray(new int[] {1, 0, 0, 1, 1}));
}
}
Running it prints:
3
5
2
0
0
2
The 5 on the second line is the window from the picture, indexes 1 to 6, with its one 0 deleted. The 2 on the third line comes from [1, 1, 1]right - left
The fourth line is 0 for [0, 0, 0]rightleftright - left[1]
The last line comes from [1, 0, 0, 1, 1]rightleft
Complexity
The work is the two edges of the window walking along the array, each only forward.
- Time: O(n), because
visits each of the n elements once, andright never moves back, so theleft loop moves it at most n times over the whole method.while - Space: O(1), because the method keeps only
,left ,right andzeros , however long the array is.best
The same window, without the final subtraction, solves