All problems

Problem 203

Rank Transform of an Array

Easy · 10 pts · Array, Sorting, Hash Table

Problem

You are given an array of N integers. Replace every element with its rank: the smallest value gets rank 1, the next larger distinct value gets rank 2, and so on. Equal values always get the same rank, and the ranks have no gaps, so the largest rank equals the number of distinct values. Sort the distinct values, give each one its position (starting from 1) in a hash map, and then look up the rank of every element in the original order.

Input

First line: integer N. Second line: N space-separated integers.

Output

Print the N ranks, space-separated.

Constraints

1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000

Example input

5
40 10 20 30 30

Example output

4 1 2 3 3

Explanation

In 40 10 20 30 30, the distinct values in order are 10, 20, 30, 40, with ranks 1, 2, 3, 4. Both 30's get rank 3, so the result is 4 1 2 3 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Distinct Elements Next: Find All Duplicates