C++ short easy code with explanation
Intuition\nThe main idea of solving this problem is that when iterate over the intervals, there are three cases:\n\n1) the new interval\'s range is before the r
# Intuition\nThe main idea of solving this problem is that when iterate over the intervals, there are three cases:\n\n1) the new interval\'s range is before the range of current interval.\nExample new interval= {3,4} and current interval= {6,9}\n\n2) the new interval is after the range of current interval.\nExample new interval= {5,7} and current interval= {2,3}\n3) the new interval range overlaps with the range of current interval.\nExample interval= {5,9} and new interval= {6,11}\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSimple iterate through intervals.\n- If case 1 occurs,ie. The new interval\'s range is before the current interval, so we can insert the new interval into answer and update new interval to the current interval.\n- If case 2 occurs,ie. The new interval is after the range of current interval, so we can simply insert the current interval into answer.\n- If case 3 occurs,ie. The new interval overlaps with current interval, so we update the first element of new interval to the minimum of current interval and new interval and update the second element of new interval to the maximum of current interval and new interval.\nExample interval= {5,9} and new interval= {6,11}. Then update new interval = {min(5,6),max(9,11)}={5,11}\n- At the end of iterations simply insert new interval to the answer.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {\n        vector<vector<int>> ans;\n        \n        for(int i=0;i<intervals.size();i++){\n            //case 1 occurs\n            if(intervals[i][0] > newInterval[1]){\n                ans.push_back(newInterval);\n                newInterval = intervals[i]; \n            }\n\n            //case 2 occurs\n            else if(intervals[i][1] < newInterval[0]){ans.push_back(intervals[i]);}       \n    \n            //case 3 occurs\n            else if(intervals[i][1] >= newInterval[0] || intervals[i][0] <= newInterval[1]){\n                newInterval[0] = min(intervals[i][0], newInterval[0]);\n                newInterval[1] = max(newInterval[1], intervals[i][1]);\n            }\n        }\n        //final step\n        ans.push_back(newInterval); \n        return ans;\n    }\n};\n```\n**Please upvote if it helped. Happy Coding!**
['C++']