Find the time taken finish Processing of given processes
Last Updated :
29 Jun, 2022
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 9
Input: 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:
- Store the arr1[i] and arr2[i] In one list and apply sorting.
- Sort on the basis of the arr2[i].
- Maintain a variable that stores the maximum time it will take for the processes to finish processing.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
static bool comp(pair< int , int > p1,
pair< int , int > p2)
{
return p1.second > p2.second;
}
int solution( int arr1[], int arr2[], int N)
{
vector<pair< int , int > > v;
for ( int i = 0; i < N; i++)
v.push_back({ arr1[i], arr2[i] });
sort(v.begin(), v.end(), comp);
int geek = 0, ans = 0;
for ( int i = 0; i < N; i++) {
geek += v[i].first;
ans = max(ans, geek + v[i].second);
}
return ans;
}
int main()
{
int arr1[] = { 1, 4, 3 };
int arr2[] = { 2, 3, 1 };
int N = sizeof (arr1) / sizeof (arr1[0]);
cout << solution(arr1, arr2, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair implements Comparable<pair>
{
int first,second;
pair( int s, int e)
{
first = s;
second = e;
}
public int compareTo(pair p)
{
return p.second - this .second;
}
}
static int solution( int arr1[], int arr2[], int N)
{
Vector<pair > v = new Vector<pair >();
for ( int i = 0 ; i < N; i++)
v.add( new pair( arr1[i], arr2[i] ));
Collections.sort(v);
int geek = 0 , ans = 0 ;
for ( int i = 0 ; i < N; i++) {
geek += v.get(i).first;
ans = Math.max(ans, geek + v.get(i).second);
}
return ans;
}
public static void main(String[] args)
{
int arr1[] = { 1 , 4 , 3 };
int arr2[] = { 2 , 3 , 1 };
int N = arr1.length;
System.out.print(solution(arr1, arr2, N));
}
}
|
Python3
from functools import cmp_to_key
def comp(p1, p2):
return p1[ 0 ] - p2[ 1 ]
def solution(arr1, arr2, N):
v = []
for i in range (N):
v.append([arr1[i], arr2[i]])
v.sort(key = cmp_to_key(comp))
geek,ans = 0 , 0
for i in range (N):
geek + = v[i][ 0 ]
ans = max (ans, geek + v[i][ 1 ])
return ans
arr1 = [ 1 , 4 , 3 ]
arr2 = [ 2 , 3 , 1 ]
N = len (arr1)
print (solution(arr1, arr2, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public class pair
{
public int first,second;
public pair( int s, int e)
{
this .first = s;
this .second = e;
}
}
static int solution( int []arr1, int []arr2, int N)
{
List<pair > v = new List<pair >();
for ( int i = 0; i < N; i++)
v.Add( new pair( arr1[i], arr2[i] ));
v.Sort((p1,p2)=>p2.second-p1.second);
int geek = 0, ans = 0;
for ( int i = 0; i < N; i++) {
geek += v[i].first;
ans = Math.Max(ans, geek + v[i].second);
}
return ans;
}
public static void Main(String[] args)
{
int []arr1 = { 1, 4, 3 };
int []arr2 = { 2, 3, 1 };
int N = arr1.Length;
Console.Write(solution(arr1, arr2, N));
}
}
|
Javascript
<script>
const comp = (p1, p2) => {
return p1[0] - p2[1];
}
const solution = (arr1, arr2, N) => {
let v = [];
for (let i = 0; i < N; i++)
v.push([arr1[i], arr2[i]]);
v.sort(comp);
let geek = 0, ans = 0;
for (let i = 0; i < N; i++) {
geek += v[i][0];
ans = Math.max(ans, geek + v[i][1]);
}
return ans;
}
let arr1 = [1, 4, 3];
let arr2 = [2, 3, 1];
let N = arr1.length;
document.write(solution(arr1, arr2, N));
</script>
|
Time complexity: O(N * logN)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...