1002. Replace Elements by Their Rank

Given an array of N integers, replace each element by its rank in the array. The rank is defined as the position of the element in the sorted unique array (1-based).

  • Smaller numbers have smaller ranks.
  • Duplicate elements have the same rank.
  • The output array should contain the ranks corresponding to each element in the original order.

Example 1:

Input: arr = [20, 15, 26, 2, 98, 6]

Output: [4, 3, 5, 1, 6, 2]

Explanation:

  • Sorted unique array is [2, 6, 15, 20, 26, 98]
  • Ranks: 2 → 1, 6 → 2, 15 → 3, 20 → 4, 26 → 5, 98 → 6

Example 2:

Input: arr = [1, 5, 8, 15, 8, 25, 9]

Output: [1, 2, 3, 5, 3, 6, 4]

Explanation:

  • Sorted unique array: [1, 5, 8, 9, 15, 25]
  • Ranks: 1 → 1, 5 → 2, 8 → 3, 9 → 4, 15 → 5, 25 → 6

Now Your Turn!

Pick the correct output for the given input

Input: arr = [40, 10, 20, 40, 30]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= N <= 105
  • -109 <= arr[i] <= 109

Fun Facts

0
class Solution {
public:
vector<int> replaceWithRank(vector<int>& arr) {
// Your code goes here
}
};
Test Case

Input:

Arr