Reverse a subarray of the given array to minimize the sum of elements at even position | Function that will give the max negative value ; Check for count greater than 0 as we require only negative solution ; Function to print the minimum sum ; Taking sum of only even index elements ; Initialize two vectors v1 , v2 ; v1 will keep account for change if 1 th index element goes to 0 ; v2 will keep account for change if 1 th index element goes to 2 ; Get the max negative value ; Driver code
def after_rev ( v ) : NEW_LINE INDENT mini = 0 NEW_LINE count = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT count += v [ i ] NEW_LINE if ( count > 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT if ( mini > count ) : NEW_LINE INDENT mini = count NEW_LINE DEDENT DEDENT return mini NEW_LINE DEDENT def print_f ( arr ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , len ( arr ) , 2 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT v1 , v2 = [ ] , [ ] NEW_LINE i = 1 NEW_LINE while i + 1 < len ( arr ) : NEW_LINE INDENT v1 . append ( arr [ i + 1 ] - arr [ i ] ) NEW_LINE i += 2 NEW_LINE DEDENT i = 1 NEW_LINE while i + 1 < len ( arr ) : NEW_LINE INDENT v2 . append ( arr [ i ] - arr [ i + 1 ] ) NEW_LINE i += 2 NEW_LINE DEDENT change = min ( after_rev ( v1 ) , after_rev ( v2 ) ) NEW_LINE if ( change < 0 ) : NEW_LINE INDENT sum += change NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 4 , 3 ] NEW_LINE print_f ( arr ) NEW_LINE DEDENT