Get Priorities after Execution 🥭
Learn this problemProblem statement
Several processes are scheduled for execution on an AWS server.
On one server, n processes are schedule where the ith process is assigned
a priority of priority[i]. The processes are placed sequentially in a queue
and are numbered 1, 2,..,n. The server schedules the processes per the following algorithm:
p. (if there is no such priority or p = 0, the algorithm is terminated)p, call them process1 and process2.process1 and removes it from the queue.process2 to floor(p/2).Given the initial priority of the processes, find the final priority of the processes which remain after the algorithm terminates.
Note that relative the arrangement of remaining processes in the queue remains the same,only their priorities change.
Function
getPrioritiesAfterExecution(priority: int[]) → int[]
Complete the function getPrioritiesAfterExecution in the editor.
getPrioritiesAfterExecution has the following parameter:
-
int priority[n]:the initial prorities of processes
Returns
-
int[]:the final priorities of the remaining processes
Examples
Example 1
priority = [6, 6, 6, 1, 2, 2]return = [3, 6, 0]
Example 2
priority = [4, 4, 2, 1]return = [0]
Example 3
priority = [2, 1, 5, 10, 10, 1]return = [0, 1]p = 10 and process1 = 4, process2 = 5. So, update the priority = floor(10/2) = 5 of process2 and remove process1. Current set of process priorities, priority = [2, 1, 5, 5, 1].
p = 5 and process1 = 3, process2 = 4. So, update the priority = floor(5/2) = 2 of process2 and remove process1. Current set of process priorities, priority = [2, 1, 2, 1].
p = 2 and process1 = 1, process2 = 3. So, update the priority = floor(2/2) = 1 of process2 and remove process1. Current set of process priorities, priority = [1, 1, 1].
p = 1 and process1 = 1, process2 = 2. So, update the priority = floor(1/2) = 0 of process2 and remove process1. Current set of process priorities, priority = [0, 1].
Constraints
1 ≤ n ≤ 10^51 ≤ priority[i] ≤ 10^9