Round Robin is a CPU Scheduling algorithm where each process is assigned a fixed time slice / quantum in a cyclic manner.
You should be aware of the following terms before proceeding:
Arrival time: Time at which the process arrives.
Burst time: The time the process takes for its execution.
Completion time: Time at which the process completes its execution.
Turn Around time: Time difference between the Completion time and the Arrival time.
Waiting time: Time difference between the Turn Around time and the Burst time.
You are given time slice / quantum and a list of processes where 'PROCESS[ i ]' denotes the Burst time of the process 'i'. Your task is to perform Round Robin scheduling and print a list of Waiting time where (i)th index of your list denotes the waiting time for 'PROCESS[ i ]'.
Note:
You can consider Arrival time for every process to be 0.
The first line contains a single integer 'T' representing the number of test cases.
The 'T' test cases are as follows:
The first line contains two integers 'N' and 'S' denoting the number of processes and time slice / quantum for performing round-robin scheduling respectively.
The second line contains 'N' space-separated integers, where the ith element denotes the Burst time for process 'i'.
Output Format :
For each test case, return the list that contains 'N' integers where ith element denotes the Waiting time for process 'i'.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^2
1 <= Time slice <= 10^2
1 <= Burst time <= 10^3
Time Limit: 1 sec
1
4 2
5 3 4 2
9 8 9 6
The pictorial representation for the scheduling will be as follows:

Waiting time for P1: 14 - 5 i.e. 9.
Waiting time for P2: 11 - 3 i.e. 8.
Waiting time for P3: 13 - 4 i.e. 9.
Waiting time for P4: 8 - 2 i.e. 6.
Thus, the output will be [ 9, 8, 9, 6 ].
1
3 2
5 4 3
7 6 8
Can you do this iteratively? Try to solve the problem by minimising the time required for the execution of the whole process.
The problem can be solved by storing the remaining burst times in a separate array say rem and iterate over all the processes again and again until all the processes are completely executed (all the elements of rem become 0).
Algorithm:
Let us understand it by an example:
For a time slice of 2 seconds and process array of size 3 as [ 5, 4, 3 ]
Initially, 'Time' = 0, rem array and 'WAITING' array are as follows:

For the first iteration of all the processes:

For the second iteration of all the processes:

For the third iteration of all the processes:

As all the processes are completed, we end our function.
O(S), where ‘S’ is the sum of burst time of all the processes.
We will iterate all the processes until all the processes are fully completed. The processes will definitely be fully completed in O(sum of burst time of all the processes).
O(N), where ‘N’ is the number of processes.
We are using an extra array to store the remaining execution time for each process.