Sum of even values and update queries on an array | Java implementation of the approach ; Function to return the sum of even elements after updating value at given index ; Add given value to A [ index ] ; To store the sum of even elements ; If current element is even ; Function to print the result for every query ; Resultant vector that stores the result for every query ; Get sum of even elements after updating value at given index ; Store sum for each query ; Print the result for every query ; Driver code
class GFG { static int EvenSum ( int [ ] A , int index , int value ) { A [ index ] = A [ index ] + value ; int sum = 0 ; for ( int i = 0 ; i < A . length ; i ++ ) if ( A [ i ] % 2 == 0 ) sum = sum + A [ i ] ; return sum ; } static void BalanceArray ( int [ ] A , int [ ] [ ] Q ) { int [ ] ANS = new int [ Q . length ] ; int i , sum ; for ( i = 0 ; i < Q . length ; i ++ ) { int index = Q [ i ] [ 0 ] ; int value = Q [ i ] [ 1 ] ; sum = EvenSum ( A , index , value ) ; ANS [ i ] = sum ; } for ( i = 0 ; i < ANS . length ; i ++ ) System . out . print ( ANS [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] [ ] Q = { { 0 , 1 } , { 1 , - 3 } , { 0 , - 4 } , { 3 , 2 } } ; BalanceArray ( A , Q ) ; } }