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
function makeArraySumEqual ( a , N ) { let count_0 = 0 , count_1 = 0 ; let odd_sum = 0 , even_sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( let i = 0 ; i < N ; i ++ ) document . write ( a [ i ] + " " ) ; } else { if ( count_0 >= N / 2 ) { for ( let i = 0 ; i < count_0 ; i ++ ) document . write ( " " ) ; } else { let is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( let i = 0 ; i < count_1 ; i ++ ) document . write ( " " ) ; } } } let arr = [ 1 , 1 , 1 , 0 ] ; let N = arr . length ; makeArraySumEqual ( arr , N ) ;