Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i ] [ 0 ] : Length of subarray v [ i ] [ 1 ] : Starting Index of subarray v [ i ] [ 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code
def maxLen ( A , N ) : NEW_LINE INDENT v = [ ] NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT if ( A [ i ] == 1 ) : NEW_LINE INDENT s = i NEW_LINE while ( i < N and A [ i ] == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT le = i - s NEW_LINE v . append ( [ le , s , i - 1 ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( len ( v ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] ) NEW_LINE if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 2 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] ) NEW_LINE DEDENT if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 1 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] - 1 ) NEW_LINE DEDENT DEDENT ans = max ( v [ len ( v ) - 1 ] [ 0 ] , ans ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLen ( arr , N ) ) NEW_LINE DEDENT