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 operationsint 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 Codeint 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 approachimport java.util.*;Â
class GFG{Â
// Function to print final array after// performing all the operationsstatic 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 Codepublic 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 operationsdef 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 Codeif __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 approachusing System;  class GFG{  // Function to print final array after// performing all the operationsstatic 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 Codepublic 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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More to that Topic: geeksforgeeks.org/count-of-packets-placed-in-each-box-after-performing-given-operations/ […]
… [Trackback]
[…] There you can find 8495 more Information to that Topic: geeksforgeeks.org/count-of-packets-placed-in-each-box-after-performing-given-operations/ […]