fork download
  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<memory.h>
  4. #include<limits.h>
  5. #include<string.h>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. char str[100005];
  10. int hashmap[300];
  11. static bool cmp(pair<int,int> p1,pair<int,int> p2)
  12. {
  13. if(p1.first<p2.first)
  14. return true;
  15. return p1.second<p2.second;
  16. }
  17. vector<pair<int,int>> helper(vector<pair<int,int>> intervalList,pair<int,int> newInterval)
  18. {
  19. // sort(intervalList.begin(),intervalList.end(),cmp);
  20. int n=intervalList.size();
  21. vector<pair<int,int>> ans;
  22. int start=newInterval.first;
  23. int end=newInterval.second;
  24.  
  25. for(int i=0;i<n;i++)
  26. {
  27. if(start>intervalList[i].second)
  28. {
  29. ans.push_back(intervalList[i]);
  30. }else if(end<intervalList[i].first)
  31. {
  32. ans.push_back({start,end});
  33. ans.push_back(intervalList[i]);
  34. }
  35. else
  36. {
  37. start=min(start,intervalList[i].first);
  38. end=max(end,intervalList[i].second);
  39. if(i==n-1)
  40. {
  41. ans.push_back({start,end});
  42. }
  43. }
  44. }
  45. return ans;
  46. }
  47.  
  48. int main()
  49. {
  50. vector<pair<int,int>> v1={{1,2},{3,5},{6,7},{8,10},{12,16}};
  51. pair<int,int> p1={4,10};
  52. vector<pair<int,int>> ans=helper(v1,p1);
  53. for(auto t:ans)
  54. {
  55. cout<<t.first<<" "<<t.second<<endl;
  56. }
  57. return 0;
  58. }
  59.  
  60.  
  61.  
  62. // You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.
  63. // Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).
  64. // Return intervals after the insertion.
  65. // Note that you don't need to modify intervals in-place. You can make a new array and return it.
  66.  
  67. // Example 1:
  68. // Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
  69. // Output: [[1,5],[6,9]]
  70. // Example 2:
  71. // Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
  72. // Output: [[1,2],[3,10],[12,16]]
  73. // Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
Success #stdin #stdout 0.01s 5272KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
1 2
3 10
12 16