Overlapping Intervals | Practice | GeeksforGeeks

Given a collection of Intervals, the task is to merge all of the overlapping Intervals.
Example 1:
Input:
Intervals = {{1,3},{2,4},{6,8},{9,10}}
Output: {{1, 4}, {6, 8}, {9, 10}}
Explanation: Given intervals: [1,3],[2,4]
[6,8],[9,10], we have only two overlapping
intervals here,[1,3] and [2,4]. Therefore
we will merge these two and return [1,4],
[6,8], [9,10].

Example 2:
Input:
Intervals = {{6,8},{1,9},{2,4},{4,7}}
Output: {{1, 9}}
Your Task:
Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(Log(N)) or O(N).
Constraints:
1 ≤ N ≤ 100
0 ≤ x ≤ y ≤ 1000
0
roy99mustang
This comment has been flagged for violating our community guidelines.
0
amanjugran12 days ago
def overlappedInterval(self, Intervals):    Intervals.sort(key=lambda x:x[0])    arr=[]    i,j = 0,len(Intervals)    while i<j:        first = Intervals[i]        if arr != []:                nex = arr[-1]                if first[0]<= nex[1]:                    arr[-1] = [nex[0],max(first[1],nex[1])]                else:                    arr.append(first)        else:            arr.append(first)        i+=1 return arr
0
ps271327131 week ago
 vector<vector<int>> overlappedInterval(vector<vector<int>>& intervals) {
       sort(intervals.begin(),intervals.end());
       vector<vector<int>>v;
       int j=0; 
       v.push_back(intervals[0]);
         for(int i=1;i<intervals.size();i++)
         {
           if(v[j][1]>=intervals[i][0]){
               if(v[j][1]<intervals[i][1])
                  v[j][1]=intervals[i][1];
           }
           else{
            v.push_back(intervals[i]);
            j++;}
         }
        return v;
   }
+1
aryansaini13101 week ago
Simplest code:
class Solution:      def swap(self, arr):       return arr[0]      def overlappedInterval(self, arr):       a=[]       arr.sort(key=self.swap)       temp=arr[0]       for i in range(n):           if arr[i][0]<=temp[1]:               temp[1]=max(temp[1], arr[i][1])           else:               a.append(temp)               temp=arr[i]                      a.append(temp)       return a
0
vishal10091 week ago
vector<vector<int>> overlappedInterval(vector<vector<int>>& arr) {
       vector<vector<int>>res;
       sort(arr.begin(),arr.end());
       int s=arr[0][0];
       int e=arr[0][1];
       for(int i=1;i<arr.size();i++){
           if(arr[i][0]<=e){
               e=max(e,arr[i][1]);
           }
           else{
               res.push_back({s,e});
               s=arr[i][0];
               e=arr[i][1];
           }
       }
       res.push_back({s,e});
       return res;
   }
0
getkar1282 weeks ago
def overlappedInterval(self, intervals):
 	#Code here
 	intervals.sort(key = lambda x: x[0])
 	output = [intervals[0]]
 	
 	for start, end in intervals[1: ]:
     	lastEnd = output[-1][1]
     	if start <= lastEnd:
         	output[-1][1] = max(lastEnd, end)
     	else:
          	output.append([start, end])
 	 return output
0
manjeetdhaterwal2 weeks ago
public int[][] overlappedInterval(int[][] Intervals)
    {
        Arrays.sort(Intervals, (a, b)-> a[0]-b[0]);
        LinkedList<int[]> merged = new LinkedList<>();
        for(int i=0; i<Intervals.length; i++)
        {
            if(merged.isEmpty() || merged.getLast()[1] < Intervals[i][0])
            {
                merged.add(Intervals[i]);
            }
            else
            {
                merged.getLast()[1] = Math.max(merged.getLast()[1], Intervals[i][1]);
            }
        }
        int res[][] = new int[merged.size()][2];
        return merged.toArray(res);
    }
0
manjeetdhaterwal
This comment was deleted.
0
crawler2 weeks ago
    vector<vector<int>> overlappedInterval(vector<vector<int>>& intervals) {
         vector<vector<int>> result;
         sort(intervals.begin(), intervals.end());
         if(intervals.size())
            result.push_back(intervals[0]);
         for(int i = 1; i < intervals.size(); i++){
             if(intervals[i][0] <= result[result.size()-1][1]){
                 int x = min(intervals[i][0], result[result.size()-1][0]);
                 int y = max(intervals[i][1], result[result.size()-1][1]);
                 result[result.size()-1][0] = x;
                 result[result.size()-1][1] = y;
             }
             else
                result.push_back(intervals[i]);
         }
         return result;
    }
+1
dip40122 weeks ago
// C++ SOLUTION
// Pls upvote if you think it is good
 
public:   vector<vector<int>> overlappedInterval(vector<vector<int>>& intervals) {       sort(intervals.begin(),intervals.end());        vector<vector<int>> ans;        vector<int> cur_interval(2);        int n=intervals.size();        cur_interval=intervals[0];        for(int i=1; i<n; i++)        {            if(cur_interval[1]<intervals[i][0])            {                ans.push_back(cur_interval);                cur_interval=intervals[i];            }            cur_interval[1]=max(cur_interval[1],intervals[i][1]);        }
        ans.push_back(cur_interval);        return ans;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.