text: Sum of even values and update queries on an array | Function to print the result for every query ; If current element is even ; If element is even then remove it from sum ; If the value becomes even after updating ; Store sum for each query ; Print the result for every query ; Driver code
code: < ? php function BalanceArray ( $ A , & $ Q ) { $ ANS = array ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < count ( $ A ) ; $ i ++ ) if ( $ A [ $ i ] % 2 == 0 ) $ sum = $ sum + $ A [ $ i ] ; for ( $ i = 0 ; $ i < count ( $ Q ) ; $ i ++ ) { $ index = $ Q [ $ i ] [ 0 ] ; $ value = $ Q [ $ i ] [ 1 ] ; if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum - $ A [ $ index ] ; $ A [ $ index ] = $ A [ $ index ] + $ value ; if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum + $ A [ $ index ] ; array_push ( $ ANS , $ sum ) ; } for ( $ i = 0 ; $ i < count ( $ ANS ) ; $ i ++ ) echo $ ANS [ $ i ] . " ▁ " ; } $ A = array ( 1 , 2 , 3 , 4 ) ; $ Q = array ( array ( 0 , 1 ) , array ( 1 , -3 ) , array ( 0 , -4 ) , array ( 3 , 2 ) ) ; BalanceArray ( $ A , $ Q ) ; ? >