VOOZH about

URL: https://www.geeksforgeeks.org/dsa/make-the-intervals-non-overlapping-by-assigning-them-to-two-different-processors/

⇱ Make the intervals non-overlapping by assigning them to two different processors - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Make the intervals non-overlapping by assigning them to two different processors

Last Updated : 12 Jul, 2025

Given a list of intervals interval[] where each interval contains two integers L and R, the task is to assign intervals to two different processors such that there are no overlapping intervals for each processor. To assign the interval[i] to the first processor, print "F" and to assign it to the second processor, print "S".
Note: If there is no possible solution print -1.
Examples: 

Input: interval[] = {{360, 480}, {420, 540}, {600, 660}} 
Output: S, F, S 
Explanation: 
The intervals assigned to processors are - 
Intervals of First Processor {{420, 540}} 
Intervals of Second Processor {{360, 480}, {600, 660}} 
As there are no overlapping intervals for each processor, it will be a valid solution.
Input: interval[] = {{99, 150}, {1, 100}, {100, 301}, {2, 5}, {150, 250}} 
Output: S, F, F, S, S 
Explanation: 
The intervals assigned to processors are - 
Intervals of First Processor {{1, 100}, {100, 301}} 
Intervals of Second Processor {{99, 150}, {2, 5}, {150, 250}} 
As there are no overlapping intervals for each processor, it will be a valid solution. 

Approach: The idea is to use Greedy algorithm to assign the intervals to the processor. 
If the highest end time of the processor is less than or equal to start time of an interval, then this interval can be assigned to the processor. Otherwise, check for the another processor. If any interval cannot be assigned to any processor then there is no possible solution. 
Below is the illustration of the steps of the approach: 

  • As in the current problem we have to print according to the order of the intervals. So to save the order of intervals, pair the intervals with their index.
  • Sort the intervals by their start time. i.e. L.
  • Iterate over the intervals and assign the intervals to the processors as follows:  
if (interval[i][0] >= firstProcessorEndTime)
 answer[interval[i]] = "F"
 firstProcessorEndTime = 
 max(firstProcessorEndTime, interval[i][0])
else if (interval[i][0] >= secondProcessorEndTime)
 answer[interval[i]] = "S"
 secondProcessorEndTime = 
 max(secondProcessorEndTime, interval[i][0])
else
 print(-1)


Below is the implementation of the above approach:


Output
S,F,S,

Time Complexity: O(NlogN)
Auxiliary Space: O(N) 

Comment