human
Longest Non | Function to find the length of the longest non - increasing subsequence.
Stores the prefix and suffix count of 1 s and 0 s respectively.
Store the number of '1' s up to current index i in pre.
Find the prefix sum.
If the current element is '1' , update the pre [ i ].
Store the number of '0' s over the range [ i , N - 1 ].
Find the suffix sum.
If the current element is '0' , update post [ i ].
Stores the maximum length.
Find the maximum value of pre [ i ] + post [ i ].
Return the answer.
Driver Code"
Solution in Python:
assistant
#Here is code to longest Non and the other steps required.
 def findLength ( str , n ) :
	pre = [ 0 ] * n
	post = [ 0 ] * n
	for i in range ( n ) :
		if ( i != 0 ) :
			pre [ i ] += pre [ i - 1 ]
		if ( str [ i ] == '1' ) :
			pre [ i ] += 1
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( i != ( n - 1 ) ) :
			post [ i ] += post [ i + 1 ]
		if ( str [ i ] == '0' ) :
			post [ i ] += 1
	ans = 0
	for i in range ( n ) :
		ans = max ( ans , pre [ i ] + post [ i ] )
	return ans
S = "0101110110100001011"
n = len ( S )
print ( findLength ( S , n ) )