Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Queues: Ticket Counter N people line up in a queue on a ticket counter to buy tickets for the new movie. Each person Xhas a

Queues: Ticket Counter
N people line up in a queue on a ticket counter to buy tickets for the new movie.
Each person Xhas a token Tx
The person who is at the front of the queue can only obtain the ticket if there is no one behind him with a token number less than that of the person at the front of the queue.
If the person in the front of the queue is able to obtain the ticket, he gets removed from the queue.
If the person at the front of the queue cannot obtain the ticket and again
lines up at the back.
Given that it takes 1 second for a person to obtain a ticket and get removed
from the queue or line up at the back, find the total tinte to process the queue of N people. Therefore, given a
queue of N people, find the number of seconds to process the queue.
Function Description
In the provided code snippet, implement the provided numSeconds (...)
method using the variables to print the number of seconds to process the queue.
You can write your code in the space below the phrase "WRITE YOUR LOGIC HERE".
There will be multiple test cases running so the Input and Output should match exactly as provided.The base Output variable result is set to a default value of -484 which can be modified. Additionally, you can add or remove these output variables.
Input Format
The first line of the input contains a number N denoting the people in the queue.
Each subsequent lines i, contains an integer Tx, denoting the token number of ith person in the queue and is stored in an array A.
Sample Input
3---- denotes N
3---- denotes token no of 1st person
3---- denotes token no of 2nd person
1---denotes token no of 3rd person
Conatraints
1<=N<=500
1<=|Tx|<=N
Output Format
The output contains a single integer denoting the number of seconds to process the queue.
Sample Output
6
Explanation
Here, the following sequence of events takes place:
1. The person with token number 3 cannot obtain the ticket, so he lines up at the back of the queue. The queue now becomes [2,1,3]
2. The person with token number 2 cannot obtain the ticket, so he lines up at
the back of the queue. The queue now becomes [1,3,2]3. The person with token number 1 obtains the ticket, so he gets removed
from the queue. The queue now becomes [3,2]4. The person with token number 3 cannot obtain the ticket, so he lines up at
the back of the queue. The queue now becomes [2,3]5. The person with token number 2 obtains the ticket, so he gets removed from the queue. The queue now becomes [3]
6. The person with token number 3 obtains the ticket, so he gets removed from the queue. The queue now becomes []
Thus, it takes a total of 6 seconds to process the queue of N people. Hence, the output is 6.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

Describe the job youd like to be doing five years from now.

Answered: 1 week ago

Question

So what disadvantages have you witnessed? (specific)

Answered: 1 week ago