Open In App

Find the time taken finish Processing of given processes

Last Updated : 29 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Comparator for sorting the vector of pair
static bool comp(pair<int, int> p1,
                 pair<int, int> p2)
{
    return p1.second > p2.second;
}
 
// Function to find the total time taken
int solution(int arr1[], int arr2[], int N)
{
    vector<pair<int, int> > v;
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
        v.push_back({ arr1[i], arr2[i] });
 
    // Sort based on time spent
    // outside critical section (arr2[])
    sort(v.begin(), v.end(), comp);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
        geek += v[i].first;
        ans = max(ans, geek + v[i].second);
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr1[] = { 1, 4, 3 };
    int arr2[] = { 2, 3, 1 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function call
    cout << solution(arr1, arr2, N);
    return 0;
}


Java




// Java program to implement the approach
import java.util.*;
 
class GFG{
 
// Comparator for sorting the vector of pair
    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;
        }
    }
 
// Function to find the total time taken
static int solution(int arr1[], int arr2[], int N)
{
    Vector<pair > v = new Vector<pair >();
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
        v.add(new pair( arr1[i], arr2[i] ));
 
    // Sort based on time spent
    // outside critical section (arr2[])
    Collections.sort(v);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
        geek += v.get(i).first;
        ans = Math.max(ans, geek + v.get(i).second);
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 1, 4, 3 };
    int arr2[] = { 2, 3, 1 };
    int N = arr1.length;
 
    // Function call
    System.out.print(solution(arr1, arr2, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program to implement the approach
 
# Comparator for sorting the vector of pair
from functools import cmp_to_key
 
def comp(p1, p2):
    return p1[0] - p2[1]
 
# Function to find the total time taken
def solution(arr1, arr2, N):
    v = []
 
    # Store all the arr1[i] and arr2[i]
    # as pair in vector
    for i in range(N):
        v.append([arr1[i], arr2[i]])
 
    # Sort based on time spent
    # outside critical section (arr2[])
    v.sort(key = cmp_to_key(comp))
    geek,ans = 0,0
 
    # Loop to calculate total time
    for i in range(N):
        geek += v[i][0]
        ans = max(ans, geek + v[i][1])
    return ans
 
# Driver code
arr1 = [1, 4, 3]
arr2 = [2, 3, 1]
N = len(arr1)
 
# Function call
print(solution(arr1, arr2, N))
 
# This code is contributed by shinjanpatra


C#




// C# program to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Comparator for sorting the vector of pair
  public class pair
  {
    public int first,second;
    public  pair(int s, int e)
    {
      this.first = s;
      this.second = e;
    }
 
 
  }
 
  // Function to find the total time taken
  static int solution(int []arr1, int []arr2, int N)
  {
    List<pair > v = new List<pair >();
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
      v.Add(new pair( arr1[i], arr2[i] ));
 
    // Sort based on time spent
    // outside critical section (arr2[])
    v.Sort((p1,p2)=>p2.second-p1.second);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
      geek += v[i].first;
      ans = Math.Max(ans, geek + v[i].second);
    }
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []arr1 = { 1, 4, 3 };
    int []arr2 = { 2, 3, 1 };
    int N = arr1.Length;
 
    // Function call
    Console.Write(solution(arr1, arr2, N));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
    // JavaScript program to implement the approach
 
    // Comparator for sorting the vector of pair
    const comp = (p1, p2) => {
        return p1[0] - p2[1];
    }
 
    // Function to find the total time taken
    const solution = (arr1, arr2, N) => {
        let v = [];
 
        // Store all the arr1[i] and arr2[i]
        // as pair in vector
        for (let i = 0; i < N; i++)
            v.push([arr1[i], arr2[i]]);
 
        // Sort based on time spent
        // outside critical section (arr2[])
        v.sort(comp);
        let geek = 0, ans = 0;
 
        // Loop to calculate total time
        for (let i = 0; i < N; i++) {
            geek += v[i][0];
            ans = Math.max(ans, geek + v[i][1]);
        }
        return ans;
    }
 
    // Driver code
 
    let arr1 = [1, 4, 3];
    let arr2 = [2, 3, 1];
    let N = arr1.length;
 
    // Function call
    document.write(solution(arr1, arr2, N));
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

9

 

Time complexity: O(N * logN)
Auxiliary Space: O(N)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads