Count of packets placed in each box after performing given operations | Function to print final array after performing avar the operations ; Initialize variables ; Traverse through avar operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
function printFinalArray ( a , n , operations , p , capacity ) { var i , curr = 0 ; var picked = false ; for ( i = 0 ; i < p ; i ++ ) { var s = operations [ i ] ; var 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 ++ ) { document . write ( a [ i ] + " " ) ; } } var capacity = 5 ; var a = [ 2 , 5 , 2 ] ; var N = a . length ; var operations = [ 3 , 2 , 4 , 1 , 4 , 5 ] ; var M = operations . length ; printFinalArray ( a , N , operations , M , capacity ) ;