Minimum increments required to make array elements alternately even and odd | Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array arr [ ] ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
def minIncr ( arr ) : NEW_LINE INDENT forEven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT if not arr [ i ] % 2 : NEW_LINE INDENT forEven += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr [ i ] % 2 : NEW_LINE INDENT forEven += 1 NEW_LINE DEDENT DEDENT DEDENT return min ( forEven , len ( arr ) - forEven ) NEW_LINE DEDENT arr = [ 1 , 4 , 6 , 8 , 9 , 5 ] NEW_LINE print ( minIncr ( arr ) ) NEW_LINE