Java fast and easy with explanation.
This is observation based.\n\n1 2 3 4 | 5 6 7\n[1...4]..\nFor a increasing straight line , (Max till i) < (Min of I + 1)...\ni.e [1..4] max =4 , [5..7] min in 5
This is observation based.\n\n1 2 3 4 | 5 6 7\n[1...4]..\nFor a increasing straight line , (Max till i) < (Min of I + 1)...\ni.e [1..4] max =4 , [5..7] min in 5 ,\nSo , If MaxTillNow < MinOnRight , then there is no element on the right side , which belongs on the left side as per above observation.\nif MaxTillNow > MinOnRight, it means , there is some element on the right side, who belongs on the left, so we CANNOT create a chunk.\n\nSo we need to Iterate through the array, each time all elements till the index are smaller (or equal) to all elements on the right, there is a new chunck.\nWe use two arrays to store the maxTillNow and minOnRight to achieve O(n) time complexity. Space complexity is O(n) too.\n\n```\nclass Solution {\n    public int maxChunksToSorted(int[] arr) {\n        \n        int n = arr.length;\n        int[] maxTillNow = new int[n];\n        int[] minOnRight = new int[n];\n        \n        maxTillNow[0] = arr[0];\n        for(int i=1; i<n; i++){\n            maxTillNow[i] = Math.max(arr[i], maxTillNow[i-1]);\n        }\n        \n        minOnRight[n-1] = arr[n-1];\n        for(int i=n-2; i>=0; i--){\n            minOnRight[i] = Math.min(arr[i], minOnRight[i+1]);\n        }\n        \n        int result = 0;\n        for(int i = 0; i < n-1; i++){\n            if(maxTillNow[i] <= minOnRight[i+1]){\n                result++;\n            }\n        }\n        return result+1;\n    }\n}\n```
['Java']