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++
#include <iostream>
using namespace std;
#define ll long long int
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;
}
|
Java
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);
}
}
|
Python3
def printFinalArray(a, n,
operations, p, capacity):
curr = 0
picked = False
for i in range (p):
s = operations[i]
flag = False
if (curr ! = 0 ):
curr - = 1
break
if (curr ! = n - 1 ):
curr + = 1
break
if (picked = = False
and a[curr] ! = 0 ):
picked = True
a[curr] - = 1
break
if (picked = = True
and a[curr] ! = capacity):
picked = False
a[curr] + = 1
break
else :
flag = True
if (flag = = True ):
break
for i in range (n):
print (a[i], end = " " )
if __name__ = = "__main__" :
capacity = 5
a = [ 2 , 5 , 2 ]
N = len (a)
operations = [ 3 , 2 , 4 , 1 , 4 , 5 ]
M = len (operations)
printFinalArray(a, N, operations,
M, capacity)
|
C#
using System;
class GFG{
static void 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 ;
break ;
}
if (flag == true )
break ;
}
for (i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
}
public static void Main()
{
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);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)
Last Updated :
14 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...