👏Beats 98.51% of users with Java || ✅Simple & Easy Well Explained Pointer Solution 🔥💥
Intuition\nAs it will be until the last index of the interval is the first index of newInterval and after that we will add the newinterval and merge after that 
# Intuition\nAs it will be until the last index of the interval is the first index of newInterval and after that we will add the newinterval and merge after that we will add the increased intervals back.\n\n# I Think This Can Help You(For Proof This Can Help You)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/c35fd081-4c96-45d7-8cb3-4046ee541869_1711904669.4915538.png)](https://leetcode.com/problems/insert-interval/submissions/1219279252/)\n\n\n# Approach\n1. Initilize List of array ans to store answer array.\n    ```\n    List<int[]> ans = new ArrayList<>();\n    ```\n2. Initilize pointer i as 0, start as newInterval\'s 0th index, end as newInterval\'s 1st index and n as intervals length.\n    ```\n    int i=0,n=intervals.length,start=newInterval[0],end=newInterval[1];\n    ```\n3. Add the intervals to the ans until the 1st index of the interval is less than the start.\n    ```\n    while(i<n && intervals[i][1]<start){\n        ans.add(new int[]{intervals[i][0],intervals[i][1]});\n        i++;\n    }\n    ```\n4. Merge intervals until the 0th index of intervals is equal to or less than end.\n    ```\n    int[] mergedList = {start,end};\n    while(i<n && intervals[i][0]<=end){\n        mergedList[0] = Math.min(intervals[i][0],mergedList[0]);\n        mergedList[1] = Math.max(intervals[i][1],mergedList[1]);\n        i++;\n    }\n    ```\n5. Add the merged intervals to the ans.\n    ```\n    ans.add(new int[]{mergedList[0],mergedList[1]});\n    ```\n6. Back interval added back in ans.\n    ```\n    while(i<n){\n        ans.add(new int[]{intervals[i][0],intervals[i][1]});\n        i++;\n    }\n    ```\n7. Transfer ans to array and return.\n    ```\n    return ans.toArray(new int[ans.size()][]);\n    ```\n\n# Complexity\n- Time complexity:\nO(n)   -->Because, we go linearly.\n\n- Space complexity:\nO(1)   --> ans List is used to store answer.\n\n# Code\n```\nclass Solution {\n    public int[][] insert(int[][] intervals, int[] newInterval) {\n        List<int[]> ans = new ArrayList<>();\n        int i=0,n=intervals.length,start=newInterval[0],end=newInterval[1];\n        while(i<n && intervals[i][1]<start){\n            ans.add(new int[]{intervals[i][0],intervals[i][1]});\n            i++;\n        }\n        int[] mergedList = {start,end};\n        while(i<n && intervals[i][0]<=end){\n            mergedList[0] = Math.min(intervals[i][0],mergedList[0]);\n            mergedList[1] = Math.max(intervals[i][1],mergedList[1]);\n            i++;\n        }\n        ans.add(new int[]{mergedList[0],mergedList[1]});\n        while(i<n){\n            ans.add(new int[]{intervals[i][0],intervals[i][1]});\n            i++;\n        }\n        return ans.toArray(new int[ans.size()][]);\n    }\n}\n```\n\n![_88a70899-aa2e-436d-8727-f561b4e9f641.jpeg](https://assets.leetcode.com/users/images/059f8859-9267-4123-b35c-8f5ae1b6e7b9_1711908090.1450307.jpeg)\n
['Array', 'Two Pointers', 'Java']