Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Divide N segments into two non-empty groups such that given condition is satisfied
Next article icon

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

Last Updated : 05 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C++
// C++ Program to divide N segments // into two non empty groups such that // given condition is satisfied  #include <bits/stdc++.h> using namespace std;  // Function to print the answer if // it exists using the concept of // merge overlapping segments void printAnswer(vector<pair<int, int> > v, int n) {     // vec[i].first -> left value     // vec[i].second.first -> right value     // vec[i].second.second -> position     vector<pair<int, pair<int, int> > > vec;     for (int i = 0; i < n; i++) {         vec.push_back({ v[i].first, { v[i].second, i } });     }     sort(vec.begin(), vec.end());      // Resultant array     //   Initialise all the values in resultant array with     //   '2' except the position at the first index of     //   sorted vec which is initialised as '1' Initialise     //   maxR to store the maximum of all right values     //   encountered so far     vector<int> res(n, 2);     int maxR = vec[0].second.first;     res[vec[0].second.second] = 1;      // If the i-th index has any point in common with the     // (i-1)th index classify it as '1'     //    in resultant array and update maxR if necessary     // else we have found the breakpoint and we can exit the     // loop      bool ok = false;     for (int i = 1; i < n; i++) {         if (maxR >= vec[i].first) {             res[vec[i].second.second]                 = res[vec[i - 1].second.second];             maxR = max(maxR, vec[i].second.first);         }         else {             ok = true;             break;         }     }     if (ok) {         for (auto x : res)             cout << x << " ";         cout << '\n';     }     else         cout << "Not possible\n"; }  int main() {     vector<pair<int, int> > v         = { { 2, 8 }, { 3, 4 }, { 5, 8 }, { 9, 10 } };     int n = (int)v.size();      printAnswer(v, n); } 
Java
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List;  public class Gfg {     public static void main(String[] args)     {         List<Pair> v = Arrays.asList(             new Pair(2, 8), new Pair(3, 4), new Pair(5, 8),             new Pair(9, 10));         int n = v.size();         printAnswer(v, n);     }      static class Pair {         int first;         int second;          public Pair(int first, int second)         {             this.first = first;             this.second = second;         }     }      static class Trio {         int first;         int second;         int third;          public Trio(int first, int second, int third)         {             this.first = first;             this.second = second;             this.third = third;         }     }      static void printAnswer(List<Pair> v, int n)     {         List<Trio> vec = new ArrayList<>();         for (int i = 0; i < n; i++) {             vec.add(new Trio(v.get(i).first,                              v.get(i).second, i));         }         vec.sort(new Comparator<Trio>() {             @Override public int compare(Trio o1, Trio o2)             {                 return o1.first - o2.first;             }         });          int[] res = new int[n];         Arrays.fill(res, 2);         int maxR = vec.get(0).second;         res[vec.get(0).third] = 1;          boolean ok = false;         for (int i = 1; i < n; i++) {             if (maxR >= vec.get(i).first) {                 res[vec.get(i).third]                     = res[vec.get(i - 1).third];                 maxR = Math.max(maxR, vec.get(i).second);             }             else {                 ok = true;                 break;             }         }         if (ok) {             for (int x : res) {                 System.out.print(x + " ");             }             System.out.println();         }         else {             System.out.println("Not possible");         }     } } 
Python3
# Python3 Program to divide N segments # into two non empty groups such that # given condition is satisfied  # Function to print the answer if # it exists using the concept of # merge overlapping segments   def printAnswer(v, n):     # Sort the indices based on their corresponding value in V     indices = list(range(n))     indices.sort(key=lambda i: v[i])      # Resultant array     # Initialise all the values in resultant array with '2'     # except the first index of 'indices' which is initialised as '1'     # Initialise maxR to store the maximum of all right values encountered so far     res = [2] * n     res[indices[0]] = 1     maxR = v[indices[0]][1]      # If the i-th index has any point in common with the (i-1)th index classify it as '1' in resultant array and update maxR if necessary     # else we have found the breakpoint and we can exit the loop     for i in range(1, n):         if maxR >= v[indices[i]][0]:             res[indices[i]] = res[indices[i-1]]             maxR = max(maxR, v[indices[i]][1])         else:             break     else:         print("Not possible")         return     print(" ".join(map(str, res)))   # Driver Code if __name__ == "__main__":      v = [[2, 8], [3, 4], [5, 8], [9, 10]]     n = len(v)     printAnswer(v, n) 
C#
using System; using System.Collections.Generic; using System.Linq;  namespace DivideNSegments {     class Program     {         static void Main(string[] args)         {             List<(int, int)> v = new List<(int, int)>()             {                 (2, 8), (3, 4), (5, 8), (9, 10)             };             int n = v.Count;              PrintAnswer(v, n);         }          static void PrintAnswer(List<(int, int)> v, int n)         {             // vec[i].Item1 -> left value             // vec[i].Item2.Item1 -> right value             // vec[i].Item2.Item2 -> position             List<(int, (int, int))> vec = new List<(int, (int, int))>();             for (int i = 0; i < n; i++)             {                 vec.Add((v[i].Item1, (v[i].Item2, i)));             }             vec = vec.OrderBy(x => x.Item1).ToList();              // Resultant array             // Initialise all the values in resultant array with '2' except the position at the first index of sorted vec which is initialised as '1'             // Initialise maxR to store the maximum of all right values encountered so far             List<int> res = Enumerable.Repeat(2, n).ToList();             int maxR = vec[0].Item2.Item1;             res[vec[0].Item2.Item2] = 1;              // If the i-th index has any point in common with the (i-1)th index classify it as '1' in resultant array and update maxR if necessary             // Else we have found the breakpoint and we can exit the loop             bool ok = false;             for (int i = 1; i < n; i++)             {                 if (maxR >= vec[i].Item1)                 {                     res[vec[i].Item2.Item2] = res[vec[i - 1].Item2.Item2];                     maxR = Math.Max(maxR, vec[i].Item2.Item1);                 }                 else                 {                     ok = true;                     break;                 }             }             if (ok)             {                 Console.WriteLine(String.Join(" ", res));             }             else             {                 Console.WriteLine("Not possible");             }         }     } } // This code has been contributed by Prince Kumar 
JavaScript
// JavaScript code to divide N segments into two non empty groups such that given condition is satisfied  // Function to print the answer if it exists using the concept of merge overlapping segments function printAnswer(v, n) {     // vec[i].first -> left value     // vec[i].second.first -> right value     // vec[i].second.second -> position     var vec = [];     for (var i = 0; i < n; i++) {         vec.push([v[i][0], [v[i][1], i]]);     }     vec.sort(function(a,b){         if(a[0] == b[0]){             return a[1][0] - b[1][0];         }         return a[0] - b[0];     });      // Resultant array     // Initialise all the values in resultant array with '2'     // except the first index of 'vec' which is initialised as '1'     // Initialise maxR to store the maximum of all right values encountered so far     var res = Array(n).fill(2);     var maxR = vec[0][1][0];     res[vec[0][1][1]] = 1;      // If the i-th index has any point in common with the (i-1)th index classify it as '1' in resultant array and update maxR if necessary     // else we have found the breakpoint and we can exit the loop     var ok = false;     for (var i = 1; i < n; i++) {         if (maxR >= vec[i][0]) {             res[vec[i][1][1]] = res[vec[i - 1][1][1]];             maxR = Math.max(maxR, vec[i][1][0]);         } else {             ok = true;             break;         }     }     if (ok) {         console.log(res.join(" "));     } else {         console.log("Not possible");     } }  // Driver Code var v = [[2, 8], [3, 4], [5, 8], [9, 10]]; var n = v.length; printAnswer(v, n);  // This code is contributed by divya_p123. 

Output
1 1 1 2 

Complexity Analysis:

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

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

N

Nishant Tanwar
Improve
Article Tags :
  • Algorithms
  • Sorting
  • Technical Scripter
  • Competitive Programming
  • Data Structures
  • DSA
  • Arrays
  • Technical Scripter 2018
Practice Tags :
  • Algorithms
  • Arrays
  • Data Structures
  • Sorting

Similar Reads

    Partition the digits of an integer such that it satisfies a given condition
    Given an integer X, the task is to partition its digit into two groups either A or B such that the sequence of digits is non decreasing when all the digits of group A are arranged followed by all the digits of group B from left to right as they appear in X. Print -1 if no such partition is possible
    13 min read
    Count of Subsets that can be partitioned into two non empty sets with equal Sum
    Given an array Arr[] of size N, the task is to find the count of subsets of Arr[] that can be partitioned into two non-empty groups having equal sum. Examples: Input: Arr[] = {2, 3, 4, 5}Output: 2Explanation: The subsets are: {2, 3, 5} which can be split into {2, 3} and {5}{2, 3, 4, 5} which can be
    15+ min read
    Minimum number of integers required such that each Segment contains at least one of them
    Given two arrays start[] and end[] consisting of positive integers denoting the starting and ending points of a segment respectively, the task is to find the minimum number of integers which lies in at least one of the given segments and each segment contains at least one of them. Examples: Input: s
    15+ min read
    Minimize group such that no two range of same group intersect
    Given an array ranges[] of size N (1 ? N ? 105), where ranges[i] = {starti, endi} represents the range between starti to endi (both inclusive). The task is to find the minimum number of groups that are needed such that each interval of ranges[] belongs to exactly one group and no two ranges[i] in th
    8 min read
    Maximum number of segments that can contain the given points
    Given an array arr[] containing N integers and two integers X and Y. Consider N line segments, where each line segment has starting and ending point as arr[i] - X and arr[i] + Y respectively. Given another array b[] of M points. The task is to assign these points to segments such that the number of
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences