- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

To solve this, we will follow these steps −

if 0 is not in nums, then

return size of nums - 1

if 1 is not in nums, then

return 0

a := a new list

cnt := 0

for each i in nums, do

if i is same as 0, then

if cnt is not same as 0, then

insert cnt at the end of a

cnt := 0

insert i at the end of a

otherwise,

cnt := cnt + 1

if cnt is not same as 0, then

insert cnt at the end of a

Max := 0

for i in range 0 to size of a, do

if a[i] is not same as 0, then

go for next iteration

if a[i] is same as 0 and i is same as size of a - 1, then

Max := maximum of Max and a[i-1]

otherwise when a[i] is same as 0 and i is same as 0, then

Max := maximum of Max and a[i+1]

otherwise when a[i] is same as 0, then

Max := maximum of Max and (a[i+1]+a[i-1])

return Max

## Example

def solve(nums): if 0 not in nums: return len(nums)-1 if 1 not in nums: return 0 a = [] cnt = 0 for i in nums: if i == 0: if cnt != 0: a.append(cnt) cnt = 0 a.append(i) else: cnt += 1 if cnt!=0: a.append(cnt) Max = 0 for i in range(len(a)): if a[i] != 0: continue if a[i] == 0 and i == len(a)-1: Max = max(Max,a[i-1]) elif a[i] == 0 and i == 0: Max = max(Max,a[i+1]) elif a[i] == 0: Max = max(Max,a[i+1]+a[i-1]) return Max nums = [1,0,1,1,1,0,1,1,0] print(solve(nums))

## Input

[1,0,1,1,1,0,1,1,0]

## Output

5

- Related Questions & Answers
- Program to find longest number of 1s after swapping one pair of bits in Python
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find longest distance of 1s in binary form of a number using Python
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find minimum amplitude after deleting K elements in Python
- Program to find minimum length of string after deleting similar ends in Python
- Program to find length of longest set of 1s by flipping k bits in Python
- Program to find longest consecutive run of 1s in binary form of n in Python
- Program to check all 1s are present one after another or not in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find remainder after dividing n number of 1s by m in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find number of substrings with only 1s using Python
- Program to find longest equivalent sublist after K increments in Python