![]() |
VOOZH | about |
Given N processes and two arrays, arr1[] and arr2[] of size N each. arr1[] contains time spent by any process in critical section and arr2[] denotes time taken by a process to complete processing after coming out of the critical section. The task is to find the time taken by all the processes to complete their processing (both in critical section and out of critical section) if processed in any order.
Note: No two processes can be using the critical section at the same instant of time but more than one process can be processed at the same instant of time outside the critical section.
Examples:
Input: N = 3, arr1[] = {1, 4, 3}, arr2[] = {2, 3, 1}
Output: 9
Explanation:
The 1st process: enters in critical section at time 0.
So after 1 unit time it completes critical section task and takes 2 more unit outside critical section.
So the total time after which 1st process is finished is 3 units.
The 2nd process: After 1 unit of time into the critical section and comes out of critical section after 5th unit.
Then spends 3 unit of time outside the critical section and finally is finished after 8th unit of time.
The 3rd process: After 5 units of time accesses the critical section and comes out after 8th unit of time.
Then spend 1 more unit outside the critical section and is finished after 9 units of time from the starting of all the processes.
So the total time taken is 9Input: N = 2, arr1[] = {2, 1}, arr2[] = {5, 2}
Output: 7
Approach: The solution to the problem is based on the concept of sorting. Follow the steps:
Below is the implementation of the above approach.
9
Time complexity: O(N * logN)
Auxiliary Space: O(N)