package leetcode;

/**
 * https://leetcode.com/problems/max-chunks-to-make-sorted/
 */
public class Problem769 {
    public int maxChunksToSorted(int[] arr) {
        int result = 0;
        int j = 0;
        int max = -1;
        for (int i = 0; i < arr.length; i++) {
            if (i <= max) {
                continue;
            }
            while (j < arr.length && arr[j] != i) {
                max = Math.max(max, arr[j]);
                j++;
            }
            if (j < arr.length) {
                max = Math.max(max, arr[j]);
            }
            result++;
        }
        return result;
    }

    public static void main(String[] args) {
        Problem769 prob = new Problem769();
//        System.out.println(prob.maxChunksToSorted(new int[]{4, 3, 2, 1, 0})); // 1
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 0, 2, 3, 4})); // 4
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 3, 2, 0, 4})); // 2
//        System.out.println(prob.maxChunksToSorted(new int[]{2, 0, 1})); // 1
//        System.out.println(prob.maxChunksToSorted(new int[]{2, 0, 1, 3})); // 2
//        System.out.println(prob.maxChunksToSorted(new int[]{0, 1})); // 2
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 0})); // 1
        System.out.println(prob.maxChunksToSorted(new int[]{1, 4, 3, 6, 0, 7, 8, 2, 5})); // 1
//        System.out.println(prob.maxChunksToSorted(new int[]{0, 3, 1, 2})); // 2
    }
}

@@ -217,16 +217,67 @@
 x = -1;%0A
+        boolean%5B%5D found = new boolean%5Barr.length%5D;%0A
         
@@ -331,16 +331,28 @@
     if (
+found%5Bi%5D && 
 i %3C= max
@@ -443,24 +443,62 @@
 %5Bj%5D != i) %7B%0A
+                found%5Barr%5Bj%5D%5D = true;%0A
             
@@ -674,891 +674,96 @@
     
-result++;%0A        %7D%0A        return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem769 prob = new Problem769();%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B4, 3, 2, 1, 0%7D)); // 1%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B1, 0, 2, 3, 4%7D)); // 4%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B1, 3, 2, 0, 4%7D)); // 2%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B2, 0, 1%7D)); // 1%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B2, 0, 1, 3%7D)); // 2%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B0, 1%7D)); // 2%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B1, 0%7D)); // 1%0A        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B1, 4, 3, 6, 0, 7, 8, 2, 5%7D)); // 1%0A//        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B0, 3, 1, 2%7D)); // 2
+if (max == j) %7B%0A                result++;%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 769