- Related Questions & Answers
- Program to find longest number of 1s after swapping one pair of bits 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 length of longest set of 1s by flipping k bits in Python
- Program to find string after deleting k consecutive duplicate characters 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
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find longest nice substring using Python
- Find last element after deleting every second element in array of n integers in C++
- Program to find range sum of sorted subarray sums using Python
- Program to find maximum ascending subarray sum using Python
- Maximize the maximum subarray sum after removing at most one element in C++
- Find all possible substrings after deleting k characters in Python

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

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

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))

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

5

Advertisements