Count of packets placed in each box after performing given operations
Given two arrays arr[] and operations[] consisting of N and M integers, and an integer C. The array arr[] represents the initial number of packets in the N boxes, each of capacity C. Starting from position 1, perform the following types of operations in the sequence specified by the array operations[]:
- Type 1: Move to the left box.
- Type 2: Move to the right box.
- Type 3:Pick a packet from the current box.
- Type 4:Drop a packet from the current box.
- Type 5:Print the number of packets in each box and exit.
Any operation is ignored in the following cases:
- Move left operation is ignored if the current position is 1.
- Move right operation is ignored if the current position is N.
- Pick a packet operation is ignored if a packet is already picked and not dropped yet or the current box is empty.
- Drop a packet operation is ignored if no packet is picked or the current box already has C packets
Examples:
Input: N = 3, C = 5, arr[] = {2, 5, 2}, M = 6, operations[] = {3, 2, 4, 1, 4, 5}
Output: {1, 5, 2}
Explanation:
Operations can be performed as follows starting from position 1:
Type 3: Pick a packet. Position = 1 and arr[] = {1, 5, 2}.
Type 2: Move right. Position = 2 and arr[] = {1, 5, 2}
Type 4: Drop packet. Position = 2 and arr[] = {1, 5, 2}
Type 1: Move left. Position = 1 and arr[] = {1, 5, 2}.
Type 4: Drop packet. Position = 1 and arr[] = {2, 5, 2}.
Type 5: Print array arr[] = {2, 5, 2}.Input: N = 3, C = 1, arr[] = {1, 1, 1}, M = 4, operations[] = {3, 2, 4, 5}
Output: {0, 1, 1}
Explanation:
Operations can be performed as follows starting from position 1:
Type 3: Pick a packet. Position = 1 and arr[] = {0, 1, 1}.
Type 2: Move right. Position = 2 and arr[] = {0, 1, 1}
Type 4: Drop packet. Position = 2 and arr[] = {0, 1, 1}
Type 5: Print array arr[] = {0, 1, 1}.
Approach: The idea is to assign variables for each task such that for the current index, initialize curr with 0. To check if the element is picked, initialize picked with false. Follow the steps below to solve the problem:
- Keep a boolean variable picked to keep track if some packet has already been picked.
- If the operation type is 1 and curr is not 0, move to the left, decrementing curr by 1.
- If the operation type is 2 and curr is not (N – 1), move to the right, incrementing current curr by 1.
- If the operation type is 3 and picked is false and the current box is not empty, decrease the packet by 1 in the current box and mark picked as true.
- If the operation type is 4 and picked is true and the current box is not full, increase the packet by 1 in the current box and mark picked as false.
- If the operation type is 5, print the current values in the array arr[] and exit.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <iostream> using namespace std; #define ll long long int // Function to print final array after // performing all the operations int printFinalArray( int * a, int n, int * operations, int p, int capacity) { // Initialize variables int i, curr = 0; bool picked = false ; // Traverse through all operations for (i = 0; i < p; i++) { // Operation Type int s = operations[i]; bool flag = false ; switch (s) { // Move left case 1: if (curr != 0) curr--; break ; // Move right case 2: if (curr != n - 1) curr++; break ; // Pick a packet case 3: if (picked == false && a[curr] != 0) { picked = true ; a[curr]--; } break ; // Drop a packet case 4: if (picked == true && a[curr] != capacity) { picked = false ; a[curr]++; } break ; // Exit default : flag = true ; } if (flag == true ) break ; } // Print final array for (i = 0; i < n; i++) { cout << a[i] << " " ; } } // Driver Code int main() { // Given capacity int capacity = 5; // Given array with initial values int a[] = { 2, 5, 2 }; // Array size int N = sizeof (a) / sizeof (a[0]); // Operations int operations[] = { 3, 2, 4, 1, 4, 5 }; // Number of operations int M = sizeof (operations) / sizeof (operations[0]); // Function call printFinalArray(a, N, operations, M, capacity); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to print final array after // performing all the operations static void printFinalArray( int []a, int n, int []operations, int p, int capacity) { // Initialize variables int i, curr = 0 ; boolean picked = false ; // Traverse through all operations for (i = 0 ; i < p; i++) { // Operation Type int s = operations[i]; boolean flag = false ; switch (s) { // Move left case 1 : if (curr != 0 ) curr--; break ; // Move right case 2 : if (curr != n - 1 ) curr++; break ; // Pick a packet case 3 : if (picked == false && a[curr] != 0 ) { picked = true ; a[curr]--; } break ; // Drop a packet case 4 : if (picked == true && a[curr] != capacity) { picked = false ; a[curr]++; } break ; // Exit default : flag = true ; } if (flag == true ) break ; } // Print final array for (i = 0 ; i < n; i++) { System.out.print(a[i] + " " ); } } // Driver Code public static void main(String[] args) { // Given capacity int capacity = 5 ; // Given array with initial values int a[] = { 2 , 5 , 2 }; // Array size int N = a.length; // Operations int operations[] = { 3 , 2 , 4 , 1 , 4 , 5 }; // Number of operations int M = operations.length; // Function call printFinalArray(a, N, operations, M, capacity); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program for the above approach # Function to print final array after # performing all the operations def printFinalArray(a, n, operations, p, capacity): # Initialize variables curr = 0 picked = False # Traverse through all operations for i in range (p): # Operation Type s = operations[i] flag = False # Move left if (curr ! = 0 ): curr - = 1 break # Move right if (curr ! = n - 1 ): curr + = 1 break # Pick a packet if (picked = = False and a[curr] ! = 0 ): picked = True a[curr] - = 1 break # Drop a packet if (picked = = True and a[curr] ! = capacity): picked = False a[curr] + = 1 break # Exit else : flag = True if (flag = = True ): break # Print final array for i in range (n): print (a[i], end = " " ) # Driver Code if __name__ = = "__main__" : # Given capacity capacity = 5 # Given array with initial values a = [ 2 , 5 , 2 ] # Array size N = len (a) # Operations operations = [ 3 , 2 , 4 , 1 , 4 , 5 ] # Number of operations M = len (operations) # Function call printFinalArray(a, N, operations, M, capacity) # This code is contributed by chitranayal |
C#
// C# program for the above approach using System; class GFG{ // Function to print final array after // performing all the operations static void printFinalArray( int []a, int n, int []operations, int p, int capacity) { // Initialize variables int i, curr = 0; bool picked = false ; // Traverse through all operations for (i = 0; i < p; i++) { // Operation Type int s = operations[i]; bool flag = false ; switch (s) { // Move left case 1: if (curr != 0) curr--; break ; // Move right case 2: if (curr != n - 1) curr++; break ; // Pick a packet case 3: if (picked == false && a[curr] != 0) { picked = true ; a[curr]--; } break ; // Drop a packet case 4: if (picked == true && a[curr] != capacity) { picked = false ; a[curr]++; } break ; // Exit default : flag = true ; break ; } if (flag == true ) break ; } // Print final array for (i = 0; i < n; i++) { Console.Write(a[i] + " " ); } } // Driver Code public static void Main() { // Given capacity int capacity = 5; // Given array with initial values int [] a = { 2, 5, 2 }; // Array size int N = a.Length; // Operations int [] operations = { 3, 2, 4, 1, 4, 5 }; // Number of operations int M = operations.Length; // Function call printFinalArray(a, N, operations, M, capacity); } } // This code is contributed by sanjoy_62 |
Javascript
<script> // javascript program for the above approach // Function to print final array after // performing all the operations function printFinalArray(a, n, operations, p, capacity) { // Initialize variables var i, curr = 0; var picked = false ; // Traverse through all operations for (i = 0; i < p; i++) { // Operation Type var s = operations[i]; var flag = false ; switch (s) { // Move left case 1: if (curr != 0) curr--; break ; // Move right case 2: if (curr != n - 1) curr++; break ; // Pick a packet case 3: if (picked == false && a[curr] != 0) { picked = true ; a[curr]--; } break ; // Drop a packet case 4: if (picked == true && a[curr] != capacity) { picked = false ; a[curr]++; } break ; // Exit default : flag = true ; } if (flag == true ) break ; } // Print final array for (i = 0; i < n; i++) { document.write(a[i] + " " ); } } // Driver Code // Given capacity var capacity = 5; // Given array with initial values var a = [ 2, 5, 2 ]; // Array size var N = a.length; // Operations var operations = [ 3, 2, 4, 1, 4, 5 ]; // Number of operations var M = operations.length; // Function call printFinalArray(a, N, operations, M, capacity); // This code is contributed by todaysgaurav. </script> |
2 5 2
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)
Contact Us