Print modified array after executing the commands of addition and subtraction | Update function for every command ; If q == 0 , add ' k ' and ' - k ' to ' l - 1' and ' r ' index ; If q == 1 , add ' - k ' and ' k ' to ' l - 1' and ' r ' index ; Function to generate the final array after executing all commands ; Generate final array with the help of DP concept ; Driver Code ; Generate final array ; Printing the final modified array
def updateQuery ( arr , n , q , l , r , k ) : NEW_LINE INDENT if ( q == 0 ) : NEW_LINE INDENT arr [ l - 1 ] += k NEW_LINE arr [ r ] += - k NEW_LINE DEDENT else : NEW_LINE INDENT arr [ l - 1 ] += - k NEW_LINE arr [ r ] += k NEW_LINE DEDENT return NEW_LINE DEDENT def generateArray ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT return NEW_LINE DEDENT n = 5 NEW_LINE arr = [ 0 for i in range ( n + 1 ) ] NEW_LINE q = 0 ; l = 1 ; r = 3 ; k = 2 NEW_LINE updateQuery ( arr , n , q , l , r , k ) NEW_LINE q , l , r , k = 1 , 3 , 5 , 3 NEW_LINE updateQuery ( arr , n , q , l , r , k ) ; NEW_LINE q , l , r , k = 0 , 2 , 5 , 1 NEW_LINE updateQuery ( arr , n , q , l , r , k ) NEW_LINE generateArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT