VOOZH about

URL: https://www.geeksforgeeks.org/dsa/divide-n-segments-into-two-non-empty-groups-such-that-given-condition-is-satisfied/

⇱ Divide N segments into two non-empty groups such that given condition is satisfied - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Divide N segments into two non-empty groups such that given condition is satisfied

Last Updated : 11 Jul, 2025

Given N segments (or ranges) represented by two non-negative integers L and R. Divide these segments into two non-empty groups such that there are no two segments from different groups that share a common point. If it is possible to do so, assign each segment a number from the set {1, 2} otherwise print Not Possible.

Examples: 

Input: arr[][] = {{5, 5}, {2, 3}, {3, 4}} 
Output: 2 1 1 
Since 2nd and 3rd segment have one point common i.e. 3, they should be contained in same group.

Input: arr[][] = {{3, 5}, {2, 3}, {1, 4}} 
Output: Not Possible 
All segments should be contained in the same group since every pair has a common point with each other. Since the other group is empty, answer is not possible. 

Prerequisites: Merge Overlapping Intervals

Approach: 

Using the concept of merging overlapping intervals, we can assign the same group to all those segments that are overlapping and alternatively changing the group number. 
To merge overlapping segments, sort all the segments with respect to their increasing left values, if left values are equal, sort it on the basis of right values first keeping order of the original indices of the segments. Then, iterate over the segments and check if any of the previous segments is overlapping with the current segment. If it does then merge it making it one segment and if it doesn't create a new one

At last, check if one of the group is empty of not. If one of them is empty, answer is not possible, otherwise print all the assigned values of segments.

Algorithm:

  1. Create a vector of pairs to store the segments with their left and right values and their original position.
  2. Sort the vector of pairs by their left values.
  3. Initialize a resultant vector with all values as 2, except for the first segment which is assigned a value of 1.
  4. Traverse the sorted vector of pairs starting from the second segment.
  5. Check if the left value of the current segment is less than or equal to the maximum right value encountered so far.
  6. If it is, then assign the same value as the previous segment to the resultant vector for the current segment and update the maximum right value encountered so far.
  7. If it is not, then we have found the breakpoint and can exit the loop.
  8. If the loop completes without finding the breakpoint, print the resultant vector.
  9. If the breakpoint is found, print "Not possible".

Below is the implementation of the above approach:


Output
1 1 1 2 

Complexity Analysis:

  • Time Complexity: O(n * log n), where n is the number of segments
  • Auxiliary Space: O(n).  
Comment
Article Tags: