Count of packets placed in each box after performing given operations | C ++ program for the above approach ; Function to print final array after performing all the operations ; Initialize variables ; Traverse through all operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
#include <iostream> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int printFinalArray ( int * a , int n , int * operations , int p , int capacity ) { int i , curr = 0 ; bool picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; bool flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int capacity = 5 ; int a [ ] = { 2 , 5 , 2 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int operations [ ] = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = sizeof ( operations ) / sizeof ( operations [ 0 ] ) ; printFinalArray ( a , N , operations , M , capacity ) ; return 0 ; }