aboutsummaryrefslogtreecommitdiff
path: root/challenge-337/deadmarshal/java/Ch1.java
blob: 1ef262c742a000d27ee1d350e8e22db550295f61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Arrays;

public class Ch1 {
  public static void main(String[] args) {
    System.out.println(Arrays.toString(smaller_than_current(new int[]{6, 5, 4, 8})));
    System.out.println(Arrays.toString(smaller_than_current(new int[]{7, 7, 7, 7})));
    System.out.println(Arrays.toString(smaller_than_current(new int[]{5, 4, 3
      , 2, 1})));
    System.out.println(Arrays.toString(smaller_than_current(new int[]{-1, 0, 3, -2, 1})));
    System.out.println(Arrays.toString(smaller_than_current(new int[]{0, 1, 1, 2, 0})));
  }

  private static int[] smaller_than_current(int[] nums) {
    int[] arr = nums.clone();
    Arrays.sort(arr);
    for (int i = 0; i < nums.length; ++i) nums[i] = search(arr, nums[i]);
    return nums;
  }

  private static int search(int[] nums, int x) {
    int l = 0, r = nums.length;
    while (l < r) {
      int mid = (l + r) / 2;
      if (nums[mid] >= x) r = mid;
      else l = mid + 1;
    }
    return l;
  }
}