Program to find longest subarray of 1s after deleting one element using Python

Suppose we have a binary array called nums, we can delete one element from it. We have to find the size of the longest non-empty subarray which is containing only 1's in the resulting array. If there is no such subarray, then return 0.

So, if the input is like nums = [1,0,1,1,1,0,1,1,0], then the output will be 5 because by removing 0 from position 5, we can get a subarray [1,1,1,1,1] there are five 1s.

Algorithm

To solve this, we will follow these steps ?

  • If 0 is not in nums, return size of nums - 1 (we must delete one element)

  • If 1 is not in nums, return 0 (no 1s available)

  • Create a list to store consecutive counts of 1s and positions of 0s

  • For each zero, calculate the maximum subarray by combining adjacent segments of 1s

  • Return the maximum length found

Example

def solve(nums):
    if 0 not in nums:
        return len(nums) - 1
    if 1 not in nums:
        return 0
    
    segments = []
    count = 0
    
    # Create segments of consecutive 1s and mark 0s
    for i in nums:
        if i == 0:
            if count != 0:
                segments.append(count)
                count = 0
            segments.append(i)
        else:
            count += 1
    
    if count != 0:
        segments.append(count)
    
    max_length = 0
    
    # Check each zero position for maximum subarray
    for i in range(len(segments)):
        if segments[i] != 0:
            continue
        
        if segments[i] == 0 and i == len(segments) - 1:
            # Zero at the end
            max_length = max(max_length, segments[i-1])
        elif segments[i] == 0 and i == 0:
            # Zero at the beginning
            max_length = max(max_length, segments[i+1])
        elif segments[i] == 0:
            # Zero in the middle - combine adjacent segments
            max_length = max(max_length, segments[i+1] + segments[i-1])
    
    return max_length

nums = [1, 0, 1, 1, 1, 0, 1, 1, 0]
print(solve(nums))

The output of the above code is ?

5

How It Works

The algorithm transforms the array into segments representing consecutive 1s and 0s. For example, [1,0,1,1,1,0,1,1,0] becomes [1, 0, 3, 0, 2, 0]. Then it checks each zero position to find the maximum possible subarray by removing that zero and combining adjacent segments of 1s.

Alternative Approach Using Sliding Window

def solve_sliding_window(nums):
    left = 0
    zeros_count = 0
    max_length = 0
    
    for right in range(len(nums)):
        if nums[right] == 0:
            zeros_count += 1
        
        # If we have more than 1 zero, shrink window from left
        while zeros_count > 1:
            if nums[left] == 0:
                zeros_count -= 1
            left += 1
        
        # Update maximum length (subtract 1 for the deleted element)
        max_length = max(max_length, right - left)
    
    return max_length

nums = [1, 0, 1, 1, 1, 0, 1, 1, 0]
print(solve_sliding_window(nums))
5

Comparison

Approach Time Complexity Space Complexity Best For
Segment Analysis O(n) O(n) Understanding the structure
Sliding Window O(n) O(1) Memory efficiency

Conclusion

Both approaches efficiently solve the problem in O(n) time. The sliding window technique is more memory-efficient, while the segment analysis approach provides clearer insight into the array structure.

Updated on: 2026-03-25T20:56:50+05:30

484 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements