Skip to content

Instantly share code, notes, and snippets.

@ykushch
Last active May 30, 2024 08:30
Show Gist options
  • Save ykushch/28e7998c4be299020232e5814b83b63f to your computer and use it in GitHub Desktop.
Save ykushch/28e7998c4be299020232e5814b83b63f to your computer and use it in GitHub Desktop.
Second task of HackerRank
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public static int[] counts(int[] nums, int[] maxes) {
Arrays.sort(nums);
List<Integer> result = new ArrayList<>();
for (int m : maxes) {
int num = searchIteratively(nums, m);
result.add(num);
}
return result.stream()
.mapToInt(i -> i)
.toArray();
}
private static int searchIteratively(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + ((high - low) / 2);
if (arr[mid] == key) {
low = mid + 1;
}
if (key < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment