Modify given array to make sum of odd and even indexed elements same | Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Given array arr [ ] ; Function call
def makeArraySumEqual ( a , N ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE odd_sum = 0 NEW_LINE even_sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if ( ( i + 1 ) % 2 == 0 ) : NEW_LINE INDENT even_sum += a [ i ] NEW_LINE DEDENT elif ( ( i + 1 ) % 2 > 0 ) : NEW_LINE INDENT odd_sum += a [ i ] NEW_LINE DEDENT DEDENT if ( odd_sum == even_sum ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( count_0 >= N / 2 ) : NEW_LINE INDENT for i in range ( count_0 ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT is_Odd = count_1 % 2 NEW_LINE count_1 -= is_Odd NEW_LINE for i in range ( count_1 ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 1 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE makeArraySumEqual ( arr , N ) NEW_LINE