class Solution { public int[] smallerNumbersThanCurrent(int[] nums) { int[] arr = Arrays.copyOf(nums, nums.length); Arrays.sort(arr); int[] res = new int[nums.length]; for (int i = 0; i < nums.length; ++i) { res[i] = binarysearch(arr, nums[i]); } return res; } public int binarysearch(int[] arr, int k) { int left = 0, right = arr.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] < k) left = mid + 1; else right = mid; } return left; } }