package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
 */
public class Problem1365 {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1365 prob = new Problem1365();
        System.out.println(Arrays.toString(prob.smallerNumbersThanCurrent(new int[]{8,1,2,2,3}))); // [4,0,1,1,3]
        System.out.println(Arrays.toString(prob.smallerNumbersThanCurrent(new int[]{6,5,4,8}))); // [2,1,0,3]
        System.out.println(Arrays.toString(prob.smallerNumbersThanCurrent(new int[]{7,7,7,7}))); // [0,0,0,0]
    }
}

package leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
 */
public class Problem1365 {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] sorted = Arrays.copyOf(nums, nums.length);
        Arrays.sort(sorted);
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < sorted.length; i++) {
            if (!map.containsKey(sorted[i])) {
                map.put(sorted[i], i);
            }
        }
        int[] answer = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            answer[i] = map.get(nums[i]);
        }
        return answer;
    }
}

Solve problem 1365