Maximize length of longest non | Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
def findLongestNonDecreasing ( A , N ) : NEW_LINE INDENT res = 0 ; NEW_LINE start = 0 ; NEW_LINE end = N - 1 ; NEW_LINE prev = - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT if ( A [ start ] <= A [ end ] ) : NEW_LINE INDENT if ( prev == - 1 ) : NEW_LINE INDENT prev = A [ start ] ; NEW_LINE res += 1 NEW_LINE start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ start ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ start ] ; NEW_LINE start += 1 NEW_LINE DEDENT elif ( A [ end ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ end ] ; NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT if ( prev == - 1 ) : NEW_LINE INDENT prev = A [ end ] ; NEW_LINE res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ end ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ end ] ; NEW_LINE end -= 1 NEW_LINE DEDENT elif ( A [ start ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ start ] ; NEW_LINE start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( findLongestNonDecreasing ( A , N ) ) ; NEW_LINE DEDENT