package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/moving-stones-until-consecutive-ii/
 */
public class Problem1040 {
    public int[] numMovesStonesII(int[] stones) {
        Arrays.sort(stones);
        int max = Math.max(stones[1] - stones[0], stones[stones.length - 1] - stones[stones.length - 2]) - 1;
        for (int i = 1; i < stones.length - 2; i++) {
            max += stones[i + 1] - stones[i] - 1;
        }
        if (max == 0) {
            return new int[]{0, max};
        }
        int min = 0;
        return new int[]{min, max};
    }

    public static void main(String[] args) {
        Problem1040 prob = new Problem1040();
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{7, 4, 9}))); // [1,2]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{6, 5, 4, 3, 10}))); // [2,3]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{100, 101, 104, 102, 103}))); // [0,0]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 2, 5}))); // [2,2]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 3, 5, 7, 10}))); // [2,4]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 12}))); // [2,5]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13}))); // [2,6]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13, 18}))); // [3,10]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13, 14}))); // [3,9]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13, 18, 23}))); // [4,14]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13, 18, 23, 24}))); // [4,17]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 5, 8, 13, 19, 23, 24}))); // [4,17]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 50, 100, 200}))); // [3,148]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{4, 8, 10, 12, 14, 20}))); // [3,8]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{1, 2, 8, 13, 21, 22, 23}))); // [4,16]
        System.out.println(Arrays.toString(prob.numMovesStonesII(new int[]{4, 5, 8, 13, 15, 16, 17}))); // [3,7]
    }
}

@@ -539,1854 +539,489 @@
 n = 
-0;%0A        return new int%5B%5D%7Bmin, max%7D
+Integer.MAX_VALUE
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1040 prob = new Problem1040();%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B7, 4, 9%7D))); // %5B1,2%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B6, 5, 4, 3, 10%7D))); // %5B2,3%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B100, 101, 104, 102, 103%7D))); // %5B0,0%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 2, 5%7D))); // %5B2,2%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 3, 5, 7, 10%7D))); // %5B2,4%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 12%7D))); // %5B2,5%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13%7D))); // %5B2,6%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13, 18%7D))); // %5B3,10%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13, 14%7D))); // %5B3,9%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13, 18, 23%7D))); // %5B4,14%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13, 18, 23, 24%7D))); // %5B4,17%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 5, 8, 13, 19, 23, 24%7D))); // %5B4,17%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 50, 100, 200%7D))); // %5B3,148%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B4, 8, 10, 12, 14, 20%7D))); // %5B3,8%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B1, 2, 8, 13, 21, 22, 23%7D))); // %5B4,16%5D%0A        System.out.println(Arrays.toString(prob.numMovesStonesII(new int%5B%5D%7B4, 5, 8, 13, 15, 16, 17%7D))); // %5B3,7%5D
+for (int i = 0, j = 0; j %3C stones.length; ++j) %7B%0A            while (stones%5Bj%5D - stones%5Bi%5D %3E= stones.length) %7B%0A                i++;%0A            %7D%0A            // corner case%0A            if (j - i + 1 == stones.length - 1 && stones%5Bj%5D - stones%5Bi%5D == stones.length - 2) %7B%0A                min = Math.min(min, 2);%0A            %7D else %7B%0A                min = Math.min(min, stones.length - (j - i + 1));%0A            %7D%0A        %7D%0A        return new int%5B%5D%7Bmin, max%7D;
 %0A   

Solve probelm 1040