diff --git a/java/1608-special-array-with-x-elements-greater-than-or-equal-x.java b/java/1608-special-array-with-x-elements-greater-than-or-equal-x.java new file mode 100644 index 000000000..dedaa91db --- /dev/null +++ b/java/1608-special-array-with-x-elements-greater-than-or-equal-x.java @@ -0,0 +1,19 @@ +class Solution { + public int specialArray(int[] nums) { + int[] count = new int[nums.length + 1]; + for (int n : nums) { + int index = n < nums.length ? n : nums.length; + count[index] += 1; + } + + int totalRight = 0; + for (int i = nums.length; i >= 0; i--) { + totalRight += count[i]; + if (i == totalRight) { + return totalRight; + } + } + + return -1; + } +}