Make all the elements of array odd by incrementing odd | Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of arr [ ] ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Print the number of operations ; Driver Code ; Function Call
def minOperations ( arr , n ) : NEW_LINE INDENT flips = 0 ; NEW_LINE i = 0 ; NEW_LINE while i < n : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT i += 2 ; NEW_LINE continue ; NEW_LINE DEDENT while ( i < n and arr [ i ] % 2 == 0 ) : NEW_LINE INDENT i += 2 ; NEW_LINE DEDENT flips += 1 ; NEW_LINE i += 2 ; NEW_LINE DEDENT i = 1 NEW_LINE while i < n : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT i += 2 ; NEW_LINE continue ; NEW_LINE DEDENT while ( i < n and arr [ i ] % 2 == 0 ) : NEW_LINE INDENT i += 2 ; NEW_LINE DEDENT flips += 1 ; NEW_LINE i += 2 ; NEW_LINE DEDENT print ( flips ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 3 , 5 , 3 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE minOperations ( arr , N ) ; NEW_LINE DEDENT