✅Optimal solution✅ || ✅O(N)✅ || 🎖Line by line commented code🎖
\n\n# Complexity\n- Time complexity:\n  O(N)\n\n- Space complexity:\n  O(N)\n\n# Code\n\nclass Solution {\npublic:\n    vector<vector<int>> insert(vector<vector
\n\n# Complexity\n- Time complexity:\n  O(N)\n\n- Space complexity:\n  O(N)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {\n        int i = 0 ;\n        int n = intervals.size();\n        vector<vector<int>>result;\n        while(i < intervals.size()){\n          if(intervals[i][1] < newInterval[0]){\n            result.push_back(intervals[i]);\n          }\n          else if(intervals[i][0] > newInterval[1]){\n            break;   \n          }\n          else{ // merging step \n        // newInterval\'s starting point will be min. of starting point of both newIntervals and intervals\n               newInterval[0] = min(newInterval[0] , intervals[i][0]);\n               newInterval[1] = max(newInterval[1] , intervals[i][1]);\n          }\n          i++;\n        }\n        result.push_back(newInterval);\n\n        while(i < n ){ // if newInterval\'s starting point will be greater than all given intervals\'s stating point(we pass throuth loop)//\n          result.push_back(intervals[i]);\n          i++;\n        }\n        return result;\n    }\n};\n```
['C++']