1493. Longest Subarray of 1’s After Deleting One Element LeetCode Solution

In this guide, you will get 1493. Longest Subarray of 1’s After Deleting One Element LeetCode Solution with the best time and space complexity. The solution to Longest Subarray of ‘s After Deleting One Element problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Subarray of ‘s After Deleting One Element solution in C++
  4. Longest Subarray of ‘s After Deleting One Element solution in Java
  5. Longest Subarray of ‘s After Deleting One Element solution in Python
  6. Additional Resources
1493. Longest Subarray of 1's After Deleting One Element LeetCode Solution image

Problem Statement of Longest Subarray of ‘s After Deleting One Element

Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1’s in the resulting array. Return 0 if there is no such subarray.

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1’s.

Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1’s is [1,1,1,1,1].

Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.

Constraints:

1 <= nums.length <= 105
nums[i] is either 0 or 1.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1493. Longest Subarray of 1’s After Deleting One Element LeetCode Solution in C++

class Solution {
 public:
  int longestSubarray(vector<int>& nums) {
    int ans = 0;
    int zeros = 0;

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      if (nums[r] == 0)
        ++zeros;
      while (zeros == 2)
        if (nums[l++] == 0)
          --zeros;
      ans = max(ans, r - l);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1493. Longest Subarray of 1’s After Deleting One Element LeetCode Solution in Java

class Solution {
  public int longestSubarray(int[] nums) {
    int ans = 0;
    int zeros = 0;

    for (int l = 0, r = 0; r < nums.length; ++r) {
      if (nums[r] == 0)
        ++zeros;
      while (zeros == 2)
        if (nums[l++] == 0)
          --zeros;
      ans = Math.max(ans, r - l);
    }

    return ans;
  }
}
// code provided by PROGIEZ

1493. Longest Subarray of 1’s After Deleting One Element LeetCode Solution in Python

class Solution:
  def longestSubarray(self, nums: list[int]) -> int:
    ans = 0
    zeros = 0

    l = 0
    for r, num in enumerate(nums):
      if num == 0:
        zeros += 1
      while zeros == 2:
        if nums[l] == 0:
          zeros -= 1
        l += 1
      ans = max(ans, r - l)

    return ans
# code by PROGIEZ

Additional Resources

See also  184. Department Highest Salary LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.