You are given an array of integers arr. Your task is to replace every value with its ranking relative to the other values in the array.
The ranking rules are:
Rank starts at 1 for the smallest number.
If two values are equal, they must receive the same rank.
Larger numbers should always have a larger rank value.
Ranks must be assigned in the most compact way, without skipping numbers.
Your job is to return the transformed array where each element is replaced by its assigned rank.
Input: arr = [12, 5, 5, 20]
Output: [3, 1, 1, 4]
Explanation:
Sorted unique values → [5, 12, 20] 5 → rank 1, 12 → rank 2, 20 → rank 3 Duplicate 5 gets the same rank.
Input: arr = [7, 15, 7, 30, 15]
Output: [1, 2, 1, 3, 2]
Input: arr = [50]
Output: [1]
Explanation:
Only one element → always rank 1.
Accepted:
Submission: