Answered step by step
Verified Expert Solution
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 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 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
denotes N
denotes token no of st person
denotes token no of nd person
denotes token no of rd person
Conatraints
N
TxN
Output Format
The output contains a single integer denoting the number of seconds to process the queue.
Sample Output
Explanation
Here, the following sequence of events takes place:
The person with token number cannot obtain the ticket, so he lines up at the back of the queue. The queue now becomes
The person with token number cannot obtain the ticket, so he lines up at
the back of the queue. The queue now becomes The person with token number obtains the ticket, so he gets removed
from the queue. The queue now becomes The person with token number cannot obtain the ticket, so he lines up at
the back of the queue. The queue now becomes The person with token number obtains the ticket, so he gets removed from the queue. The queue now becomes
The person with token number obtains the ticket, so he gets removed from the queue. The queue now becomes
Thus, it takes a total of seconds to process the queue of N people. Hence, the output is
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started