Understand Longest Subarray of 1's After Deleting One Element Problem

Longest Subarray of 1's After Deleting One Element

Medium
Topics: Array, Sliding Window, Two Pointers


Given a binary array nums, delete exactly one element.

Return the size of the longest non-empty subarray containing only 1s in the resulting array.
Return 0 if there is no such subarray.


Examples

Example 1

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the element at index 2 (0-based), the array becomes [1,1,1], which has length 3.


Example 2

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: Delete the element at index 4. One longest subarray of 1's is [1,1,1,1,1], length 5.


Example 3

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element, so the best you can get is length 2.


Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1
Category:
  • Arrays
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/

Java
Output:

Loading component...

Loading component...