From 3f021b70f4443d74973448e93f2dc8618fc93f79 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 15 Jan 2021 19:39:21 -0600 Subject: [PATCH] special-array-with-x-elements-greater-than-or-equal-x solution java --- .../Solution.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 special-array-with-x-elements-greater-than-or-equal-x/Solution.java diff --git a/special-array-with-x-elements-greater-than-or-equal-x/Solution.java b/special-array-with-x-elements-greater-than-or-equal-x/Solution.java new file mode 100644 index 0000000..319291b --- /dev/null +++ b/special-array-with-x-elements-greater-than-or-equal-x/Solution.java @@ -0,0 +1,34 @@ +// Accepted +// Runtime: 1 ms +// Memory Usage: 39.3 MB +// Submitted: January 15th, 2021 + +class Solution { + public int specialArray(int[] nums) { + // Get the largest integer in the array + int max = nums[0]; + for (int i = 1; i < nums.length; i++) + if (nums[i] > max) + max = nums[i]; + + for (int i = 0; i <= max; i++) { + int count = 0; + + // Count the number of integers greater than or equal + for (int j = 0; j < nums.length; j++) { + if (nums[j] >= i) + count++; + + // If we go over, stop counting + if (count > i) + break; + } + + // If it's equal, we found our answer + if (count == i) + return i; + } + + return -1; + } +} \ No newline at end of file