Rearrange array such that all even | Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
def checkPossible ( a , n ) : NEW_LINE INDENT even_no_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even_no_count += 1 NEW_LINE DEDENT DEDENT if ( n // 2 > even_no_count ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT print ( " Yes " ) NEW_LINE j = 0 NEW_LINE for i in range ( 1 , n , 2 ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT while ( j < n and a [ j ] % 2 != 0 ) : NEW_LINE INDENT j += 2 NEW_LINE DEDENT a [ i ] += a [ j ] NEW_LINE a [ j ] = a [ i ] - a [ j ] NEW_LINE a [ i ] -= a [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE checkPossible ( arr , n ) NEW_LINE