Count of packets placed in each box after performing given operations | Java program for the above approach ; Function to print final array after performing aint the operations ; Initialize variables ; Traverse through aint 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
import java . util . * ; class GFG { static void printFinalArray ( int [ ] a , int n , int [ ] operations , int p , int capacity ) { int i , curr = 0 ; boolean picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; boolean 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 ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int capacity = 5 ; int a [ ] = { 2 , 5 , 2 } ; int N = a . length ; int operations [ ] = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = operations . length ; printFinalArray ( a , N , operations , M , capacity ) ; } }