{"id":10013,"date":"2022-09-27T10:09:58","date_gmt":"2022-09-27T10:09:58","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=10013"},"modified":"2022-10-10T09:27:32","modified_gmt":"2022-10-10T09:27:32","slug":"merge-overlapping-intervals","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/","title":{"rendered":"Merge Overlapping Intervals"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\" alt=\"\" \/><\/p>\n<h3>Problem Statement<\/h3>\n<p>You will be given an <a href=\"https:\/\/prepbytes.com\/blog\/tag\/arrays\/\" title=\"array\">array<\/a> of time intervals that will be in any random order. You have to merge all the overlapping intervals and print only the mutually exclusive intervals i.e. the non-overlapping intervals.<\/p>\n<h3>Example<\/h3>\n<p>Consider the example shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182034365-1-01.png\" alt=\"\" \/><\/p>\n<p>So, in the above example, {10,30} overlaps with {20,40} since the starting time of interval {20,40} i.e. 20 is not greater than the end time of the interval {10,30}. Hence, we merge them to form the interval {10,40}. <\/p>\n<h3>Merge Overlapping Intervals Brute Force Approach<\/h3>\n<p>Let us first understand the brute force approach for merging the overlapping intervals. It is straightforward. We will take each interval once and compare it with all the other intervals. If it overlaps with some interval, we merge them. So, in this approach, we are taking an interval out of N intervals and comparing it with the rest N-1 intervals. In the worst case, we will have to do this for all N intervals (no overlapping intervals exist in the worst case). So, the time complexity will be O(N2). Hence, we will not implement (code) this approach. Let us think of something better.<\/p>\n<h3>Merge Overlapping Intervals Using Stack<\/h3>\n<p>Consider the following intervals shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182050365-1-02.png\" alt=\"\" \/><\/p>\n<p>So, let us try to merge the overlapping intervals in this array. First of all, we will sort the array based on increasing the order of start time as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182065092-1-03.png\" alt=\"\" \/><\/p>\n<p>Now, we will take an empty stack and push the first interval within the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182081332-1-04.png\" alt=\"\" \/><br \/>\nNow, the next interval is 5-12. Since the start time of the next interval i.e. 5 is less than the end time of the interval present at the top of the stack, this means that these intervals are overlapping. Hence, we have to merge them.<\/p>\n<p>So, we compare the intervals and find out that the end time of the second interval i.e. 12 is greater than the end time of the interval present at the top of the stack. So, we will change the end time of the interval present at the top of the stack to 12 and hence the intervals are merged.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182096128-1-05.png\" alt=\"\" \/><\/p>\n<p>Now, we move to the next interval i.e. 14-19. Here, the start of this interval is greater than the end of the interval present at the top of the stack. So, they are non-overlapping and we will simply add 14-19 to the top of the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182109815-1-06.png\" alt=\"\" \/><\/p>\n<p>We added 14-19 to the top of the stack because we are sure that no interval now will merge with intervals 1-12. This is because the intervals have been sorted concerning their start times and if the start time of the interval 14-19 is not less than the end time of 1-12, the further intervals will have a start time greater than 14 and will not merge with 1-12.<\/p>\n<p>So, the next interval is 22-28. The start time of this interval is also greater than the end time of the interval present at the top of the stack i.e. 22 is greater than 19. So, these are non-overlapping intervals and we will add 22-28 to the top of the stack now.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182123856-1-07.png\" alt=\"\" \/><\/p>\n<p>Next, we have the intervals 25-27. This interval has a start time less than the end time of the interval present at the top of the stack i.e. 25 is less than 28. So, these intervals are overlapping and we will have to merge them.<\/p>\n<p>This time, the end time of the interval at the top of the stack i.e. 28 is greater than the end time of the current interval i.e. 27. So, the current interval will be merged with the top of the stack interval, and will completely be overlapped by the interval at the top of the stack i.e. the end time of the top of the stack will not change as 25-27 lies completely inside 22-28.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182133727-1-08.png\" alt=\"\" \/><\/p>\n<p>Now, the last interval 27-30 is considered. This interval has a start time less than the end time of the interval at the top of the stack. So, merging will take place here as well.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182144331-1-09.png\" alt=\"\" \/><\/p>\n<p>So, the reverse order of the intervals now presents in the stack are non-overlapping intervals in sorted order.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182157471-1-10.png\" alt=\"\" \/><\/p>\n<p>So, this is how we can solve this problem in an optimized approach using Stack and sorting. Now that we have understood this approach, let us write the code for the same.<\/p>\n<h3>,Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_10016 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_10016 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_10016 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10016 .wpsm_nav-tabs > li.active > a, #tab_container_10016 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10016 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_10016 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_10016 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10016 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10016 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10016 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10016 .wpsm_nav-tabs > li > a:hover , #tab_container_10016 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_10016 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_10016 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_10016 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_10016 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_10016 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_10016 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_10016 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10016 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_10016 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10016 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_10016 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_10016\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10016\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_10016_1\" aria-controls=\"tabs_desc_10016_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_10016_2\" aria-controls=\"tabs_desc_10016_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_10016\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_10016_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\n\r\nstruct Interval {\r\n\tint start, end;\r\n};\r\n\r\n\r\nbool compareInterval(Interval i1, Interval i2)\r\n{\r\n\treturn (i1.start &lt; i2.start);\r\n}\r\n\r\nvoid mergeIntervals(Interval arr[], int n)\r\n{\r\n    \r\n\tif (n &lt;= 0)\r\n\t\treturn;\r\n\r\n\t\r\n\tstack&lt;Interval&gt; s;\r\n\t\r\n\tsort(arr, arr + n, compareInterval);\r\n\r\n\ts.push(arr[0]);\r\n\r\n\tfor (int i = 1; i &lt; n; i++) {\r\n\t    \r\n\t\tInterval top = s.top();\r\n\r\n\t\tif (top.end &lt; arr[i].start)\r\n\t\t\ts.push(arr[i]);\r\n\r\n\t\telse if (top.end &lt; arr[i].end) {\r\n\t\t\ttop.end = arr[i].end;\r\n\t\t\ts.pop();\r\n\t\t\ts.push(top);\r\n\t\t}\r\n\t}\r\n\tcout &lt;&lt; &quot;&#92;n The Merged Intervals are: &quot;;\r\n\twhile (!s.empty()) {\r\n\t\tInterval t = s.top();\r\n\t\tcout &lt;&lt; &quot;[&quot; &lt;&lt; t.start &lt;&lt; &quot;,&quot; &lt;&lt; t.end &lt;&lt; &quot;] &quot;;\r\n\t\ts.pop();\r\n\t}\r\n\treturn;\r\n}\r\n\r\nint main()\r\n{\r\n\tInterval arr[]\r\n\t\t= { { 1, 8 }, { 5, 12 }, { 14, 19 }, { 22, 28 }, {25, 27} , {27, 30} };\r\n\tint n = sizeof(arr) \/ sizeof(arr[0]);\r\n\tmergeIntervals(arr, n);\r\n\treturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_10016_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static void mergeIntervals(Interval arr[])\r\n\t{\r\n\t    \r\n\t\tif (arr.length &lt;= 0)\r\n\t\t\treturn;\r\n\r\n\t\tStack&lt;Interval&gt; stack=new Stack&lt;&gt;();\r\n\r\n\t\tArrays.sort(arr,new Comparator&lt;Interval&gt;(){\r\n\t\t\tpublic int compare(Interval i1,Interval i2)\r\n\t\t\t{\r\n\t\t\t\treturn i1.start-i2.start;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tstack.push(arr[0]);\r\n\r\n\t\tfor (int i = 1 ; i &lt; arr.length; i++)\r\n\t\t{\r\n\t\t\tInterval top = stack.peek();\r\n\r\n\t\t\tif (top.end &lt; arr[i].start)\r\n\t\t\t\tstack.push(arr[i]);\r\n\r\n\t\t\telse if (top.end &lt; arr[i].end)\r\n\t\t\t{\r\n\t\t\t\ttop.end = arr[i].end;\r\n\t\t\t\tstack.pop();\r\n\t\t\t\tstack.push(top);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tSystem.out.print(&quot;The Merged Intervals are: &quot;);\r\n\t\twhile (!stack.isEmpty())\r\n\t\t{\r\n\t\t\tInterval t = stack.pop();\r\n\t\t\tSystem.out.print(&quot;[&quot;+t.start+&quot;,&quot;+t.end+&quot;] &quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String args[]) {\r\n\t\tInterval arr[]=new Interval[6];\r\n\t\tarr[0]=new Interval(1,8);\r\n\t\tarr[1]=new Interval(5,12);\r\n\t\tarr[2]=new Interval(14,19);\r\n\t\tarr[3]=new Interval(22,28);\r\n\t\tarr[4]=new Interval(25,27);\r\n\t\tarr[5]=new Interval(27,20);\r\n\t\tmergeIntervals(arr);\r\n\t}\r\n}\r\n\r\nclass Interval\r\n{\r\n\tint start,end;\r\n\tInterval(int start, int end)\r\n\t{\r\n\t\tthis.start=start;\r\n\t\tthis.end=end;\r\n\t}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_10016 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_10016 a\"),jQuery(\"#tab-content_10016\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Time Complexity<\/strong><\/p>\n<p>The time complexity of this approach is O(N <em> LogN) because we are sorting the array first. The time complexity of working with Stack is O(N) but due to sitting the time complexity becomes O(N <\/em> LogN).<\/p>\n<p><strong>Space Complexity<\/strong><\/p>\n<p>The space complexity of this approach is O(N) as we are using a stack to solve the problem.<\/p>\n<h3>Merge Intervals Space Optimized<\/h3>\n<p>So, we saw how we can merge the intervals using stack and sorting. However, if we look carefully, we don\u2019t need a Stack here. We can do the merging in-place without needing a stack as follows.<\/p>\n<p>Except for the first interval, if the current interval overlaps with the previous interval, we merge them and keep doing this till we can merge the intervals.<br \/>\nIf the intervals don\u2019t merge, add them to the output array or list.<\/p>\n<p>The code for the above approach is given below.<\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_10018 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_10018 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_10018 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_10018 .wpsm_nav-tabs > li.active > a, #tab_container_10018 .wpsm_nav-tabs > li.active > a:hover, #tab_container_10018 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_10018 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_10018 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_10018 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_10018 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_10018 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_10018 .wpsm_nav-tabs > li > a:hover , #tab_container_10018 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_10018 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_10018 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_10018 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_10018 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_10018 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_10018 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_10018 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10018 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_10018 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_10018 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_10018 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_10018\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_10018\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_10018_1\" aria-controls=\"tabs_desc_10018_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_10018_2\" aria-controls=\"tabs_desc_10018_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_10018\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_10018_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nstruct Interval {\r\n\tint s, e;\r\n};\r\n\r\nbool mycomp(Interval a, Interval b) { return a.s &lt; b.s; }\r\n\r\nvoid mergeIntervals(Interval arr[], int n)\r\n{\r\n\tsort(arr, arr + n, mycomp);\r\n\r\n\tint index = 0;\r\n\t\r\n\tfor (int i = 1; i &lt; n; i++) {\r\n\t    \r\n\t\tif (arr[index].e &gt;= arr[i].s) {\r\n\t\t\tarr[index].e = max(arr[index].e, arr[i].e);\r\n\t\t}\r\n\t\telse {\r\n\t\t\tindex++;\r\n\t\t\tarr[index] = arr[i];\r\n\t\t}\r\n\t}\r\n\r\n\tcout &lt;&lt; &quot;&#92;n The Merged Intervals are: &quot;;\r\n\tfor (int i = 0; i &lt;= index; i++)\r\n\t\tcout &lt;&lt; &quot;[&quot; &lt;&lt; arr[i].s &lt;&lt; &quot;, &quot; &lt;&lt; arr[i].e &lt;&lt; &quot;] &quot;;\r\n}\r\n\r\nint main()\r\n{\r\nInterval arr[]\r\n\t\t= { { 1, 8 }, { 5, 12 }, { 14, 19 }, { 22, 28 }, {25, 27} , {27, 30} };\r\n\tint n = sizeof(arr) \/ sizeof(arr[0]);\r\n\tmergeIntervals(arr, n);\r\n\treturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_10018_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\n\r\nclass Interval\r\n{\r\n\tint start,end;\r\n\t\r\n\tInterval(int start, int end)\r\n\t{\r\n\t\tthis.start=start;\r\n\t\tthis.end=end;\r\n\t}\r\n}\r\n\r\npublic class Main {\r\n\t\r\n\tpublic static void mergeIntervals(Interval arr[])\r\n\t{\r\n\t\tArrays.sort(arr,new Comparator&lt;Interval&gt;(){\r\n\t\t\tpublic int compare(Interval i1,Interval i2)\r\n\t\t\t{\r\n\t\t\t\treturn i1.start - i2.start;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tint index = 0; \r\n\t\t\r\n\t\tfor (int i=1; i&lt;arr.length; i++)\r\n\t\t{\r\n\t\t\tif (arr[index].end &gt;= arr[i].start)\r\n\t\t\t{\r\n\t\t\t\tarr[index].end = Math.max(arr[index].end, arr[i].end);\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tindex++;\r\n\t\t\t\tarr[index] = arr[i];\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.print(&quot;The Merged Intervals are: &quot;);\r\n\t\tfor (int i = 0; i &lt;= index; i++)\r\n\t\t{\r\n\t\t\tSystem.out.print(&quot;[&quot; + arr[i].start + &quot;,&quot;+ arr[i].end + &quot;]&#92;t&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String args[]) {\r\n\t\tInterval arr[]=new Interval[6];\r\n\t\tarr[0]=new Interval(1,8);\r\n\t\tarr[1]=new Interval(5,12);\r\n\t\tarr[2]=new Interval(14,19);\r\n\t\tarr[3]=new Interval(22,28);\r\n\t\tarr[4]=new Interval(25,27);\r\n\t\tarr[5]=new Interval(27,20);\r\n\t\tmergeIntervals(arr);\r\n\t}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_10018 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_10018 a\"),jQuery(\"#tab-content_10018\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p>We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals, <a href=\"http:\/\/https:\/\/www.prepbytes.com\/\" title=\"PrepBytes\">PrepBytes<\/a> also provides a good collection of <a href=\"https:\/\/www.prepbytes.com\/prepbytes-courses\" title=\"Foundation Courses\">Foundation Courses<\/a> that can help you enhance your coding skills. Want to make sure you ace the interview in one go? Join our <a href=\"https:\/\/www.prepbytes.com\/placement-preparation-program\" title=\"Placement Program\">Placement Program<\/a> which will help you get prepared and land your dream job at MNCs. Mentors of Prepbytes are highly experienced and can provide you with basic, in-depth subject knowledge for better understanding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement You will be given an array of time intervals that will be in any random order. You have to merge all the overlapping intervals and print only the mutually exclusive intervals i.e. the non-overlapping intervals. Example Consider the example shown below. So, in the above example, {10,30} overlaps with {20,40} since the starting [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10013","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Merge Overlapping Intervals | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge Overlapping Intervals | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-27T10:09:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-10T09:27:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Merge Overlapping Intervals\",\"datePublished\":\"2022-09-27T10:09:58+00:00\",\"dateModified\":\"2022-10-10T09:27:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\"},\"wordCount\":966,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\",\"articleSection\":[\"Miscellaneous\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\",\"name\":\"Merge Overlapping Intervals | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\",\"datePublished\":\"2022-09-27T10:09:58+00:00\",\"dateModified\":\"2022-10-10T09:27:32+00:00\",\"description\":\"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Miscellaneous\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/miscellaneous\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Merge Overlapping Intervals\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Merge Overlapping Intervals | PrepBytes Blog","description":"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/","og_locale":"en_US","og_type":"article","og_title":"Merge Overlapping Intervals | PrepBytes Blog","og_description":"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.","og_url":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-09-27T10:09:58+00:00","article_modified_time":"2022-10-10T09:27:32+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Merge Overlapping Intervals","datePublished":"2022-09-27T10:09:58+00:00","dateModified":"2022-10-10T09:27:32+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/"},"wordCount":966,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg","articleSection":["Miscellaneous"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/","url":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/","name":"Merge Overlapping Intervals | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg","datePublished":"2022-09-27T10:09:58+00:00","dateModified":"2022-10-10T09:27:32+00:00","description":"We tried to discuss Merge Overlapping Intervals. We hope this article gives you a better understanding of Merge Overlapping Intervals.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664182170018-Topic.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/merge-overlapping-intervals\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Miscellaneous","item":"https:\/\/prepbytes.com\/blog\/category\/miscellaneous\/"},{"@type":"ListItem","position":3,"name":"Merge Overlapping Intervals"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=10013"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10013\/revisions"}],"predecessor-version":[{"id":10179,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/10013\/revisions\/10179"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=10013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=10013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=10013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}