Last active
May 30, 2024 08:30
-
-
Save ykushch/28e7998c4be299020232e5814b83b63f to your computer and use it in GitHub Desktop.
Second task of HackerRank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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