Print modified array after executing the commands of addition and subtraction | C ++ program to find modified array after executing m commands / queries ; 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 program ; Generate final array ; Printing the final modified array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void updateQuery ( int arr [ ] , int n , int q , int l , int r , int k ) { if ( q == 0 ) { arr [ l - 1 ] += k ; arr [ r ] += - k ; } else { arr [ l - 1 ] += - k ; arr [ r ] += k ; } return ; } void generateArray ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; ++ i ) arr [ i ] += arr [ i - 1 ] ; return ; } int main ( ) { int n = 5 ; int arr [ n + 1 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; int q = 0 , l = 1 , r = 3 , k = 2 ; updateQuery ( arr , n , q , l , r , k ) ; q = 1 , l = 3 , r = 5 , k = 3 ; updateQuery ( arr , n , q , l , r , k ) ; q = 0 , l = 2 , r = 5 , k = 1 ; updateQuery ( arr , n , q , l , r , k ) ; generateArray ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; return 0 ; }