package leetcode;

/**
 * https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/
 */
public class Problem1608 {
    public int specialArray(int[] nums) {
        // TODO
        return 0;
    }

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

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -215,427 +215,473 @@
     
-// TODO%0A        return 0;
+Arrays.sort(nums);%0A        for (int i = 1; i %3C= nums.length; i++) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1608 prob = new Problem1608();%0A        System.out.println(prob.specialArray(new int%5B%5D%7B3, 5%7D)); // 2%0A        System.out.println(prob.specialArray(new int%5B%5D%7B0, 0%7D)); // -1%0A        System.out.println(prob.specialArray(new int%5B%5D%7B0, 4, 3, 0, 4%7D)); // 3%0A        System.out.println(prob.specialArray(new int%5B%5D%7B3, 6, 7, 7, 0%7D)); //
+    int index = Arrays.binarySearch(nums, i);%0A            if (index %3C 0) %7B%0A                index = -(index) - 1;%0A            %7D else %7B%0A                while (index %3E= 0 && nums%5Bindex%5D == i) %7B%0A                    index--;%0A                %7D%0A                index++;%0A            %7D%0A            if (nums.length - index == i) %7B%0A                return i;%0A            %7D%0A        %7D%0A        return
  -1
+;
 %0A   

Solve problem 1608