# Job Scheduling with two jobs allowed at a time

We are given N jobs, and their starting and ending times. We can do two jobs simultaneously at a particular moment. If one job ends at the same moment some other show starts then we can’t do them. We need to check if it is possible to complete all the jobs or not.

**Examples:**

Input : Start and End times of Jobs 1 2 2 3 4 5 Output : Yes By the time third job starts, both jobs are finished. So we can schedule third job. Input : Start and End times of Jobs 1 5 2 4 2 6 1 7 Output : No All 4 jobs needs to be scheduled at time 3 which is not possible.

We first sort the jobs according to their starting time. Then we start two jobs simultaneously and check if the starting time of third job and so on is greater than the ending time of and of the previous two jobs.

The implementation the above idea is given below.

## C++

`// CPP program to check if all jobs can be scheduled ` `// if two jobs are allowed at a time. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `checkJobs(` `int` `startin[], ` `int` `endin[], ` `int` `n) ` `{ ` ` ` `// making a pair of starting and ending time of job ` ` ` `vector<pair<` `int` `, ` `int` `> > a; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `a.push_back(make_pair(startin[i], endin[i])); ` ` ` ` ` `// sorting according to starting time of job ` ` ` `sort(a.begin(), a.end()); ` ` ` ` ` `// starting first and second job simultaneously ` ` ` `long` `long` `tv1 = a[0].second, tv2 = a[1].second; ` ` ` ` ` `for` `(` `int` `i = 2; i < n; i++) { ` ` ` ` ` `// Checking if starting time of next new job ` ` ` `// is greater than ending time of currently ` ` ` `// scheduled first job ` ` ` `if` `(a[i].first >= tv1) ` ` ` `{ ` ` ` `tv2 = tv1; ` ` ` `tv1 = a[i].second; ` ` ` `} ` ` ` ` ` `// Checking if starting time of next new job ` ` ` `// is greater than ending time of ocurrently ` ` ` `// scheduled second job ` ` ` `else` `if` `(a[i].first >= tv2) ` ` ` `tv2 = a[i].second; ` ` ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `startin[] = { 1, 2, 4 }; ` `// starting time of jobs ` ` ` `int` `endin[] = { 2, 3, 5 }; ` `// ending times of jobs ` ` ` `int` `n = ` `sizeof` `(startin) / ` `sizeof` `(startin[0]); ` ` ` `cout << checkJobs(startin, endin, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check if all ` `# jobs can be scheduled if two jobs ` `# are allowed at a time. ` ` ` `def` `checkJobs(startin, endin, n): ` ` ` ` ` `# making a pair of starting and ` ` ` `# ending time of job ` ` ` `a ` `=` `[] ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `a.append([startin[i], endin[i]]) ` ` ` ` ` `# sorting according to starting ` ` ` `# time of job ` ` ` `a.sort() ` ` ` ` ` `# starting first and second job ` ` ` `# simultaneously ` ` ` `tv1 ` `=` `a[` `0` `][` `1` `] ` ` ` `tv2 ` `=` `a[` `1` `][` `1` `] ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n): ` ` ` ` ` `# Checking if starting time of next ` ` ` `# new job is greater than ending time ` ` ` `# of currently scheduled first job ` ` ` `if` `(a[i][` `0` `] >` `=` `tv1) : ` ` ` ` ` `tv2 ` `=` `tv1 ` ` ` `tv1 ` `=` `a[i][` `1` `] ` ` ` ` ` `# Checking if starting time of next ` ` ` `# new job is greater than ending time ` ` ` `# of ocurrently scheduled second job ` ` ` `elif` `(a[i][` `0` `] >` `=` `tv2) : ` ` ` `tv2 ` `=` `a[i][` `1` `] ` ` ` ` ` `else` `: ` ` ` `return` `0` ` ` ` ` `return` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `startin ` `=` `[` `1` `, ` `2` `, ` `4` `] ` `# starting time of jobs ` ` ` `endin ` `=` `[` `2` `, ` `3` `, ` `5` `] ` `# ending times of jobs ` ` ` `n ` `=` `3` ` ` `print` `(checkJobs(startin, endin, n)) ` ` ` `# This code is contributed by ` `# Shubham Singh(SHUBHAMSINGH10) ` |

*chevron_right*

*filter_none*

**Output:**

1

An alternate solution is to find maximum number of jobs that needs to be scheduled at any time. If this count is more than 2, return false. Else return true.

This article is contributed by **Sarthak Kohli**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find minimum time to finish all jobs with given constraints
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimize the sum of product of two arrays with permutations allowed
- Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)
- Scheduling priority tasks in limited time and minimizing loss
- Schedule jobs so that each server gets equal load
- Longest Remaining Time First (LRTF) CPU Scheduling Program
- Minimum time to reach a point with +t and -t moves at time t
- Minimize Cost with Replacement with other allowed
- Minimum cost to reach a point N from 0 with two different operations allowed
- Find the minimum capacity of the train required to hold the passengers
- Count of numbers in the range [L, R] which satisfy the given conditions