Relative Ranks - Problem

You are given an integer array score of size n, where score[i] is the score of the i-th athlete in a competition. All the scores are guaranteed to be unique.

The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:

  • The 1st place athlete's rank is "Gold Medal"
  • The 2nd place athlete's rank is "Silver Medal"
  • The 3rd place athlete's rank is "Bronze Medal"
  • For the 4th place to the nth place athlete, their rank is their placement number (i.e., the x-th place athlete's rank is "x")

Return an array answer of size n where answer[i] is the rank of the i-th athlete.

Input & Output

Example 1 — Basic Ranking
$ Input: score = [5,4,3,2,1]
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
💡 Note: Score 5 is highest (Gold), 4 is second (Silver), 3 is third (Bronze), 2 gets rank "4", 1 gets rank "5"
Example 2 — Unordered Scores
$ Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
💡 Note: 10 is highest (Gold at index 0), 9 is second highest (Silver at index 3), 8 is third highest (Bronze at index 2), 4 gets "4", 3 gets "5"
Example 3 — Minimum Size
$ Input: score = [1]
Output: ["Gold Medal"]
💡 Note: Single athlete automatically gets first place and Gold Medal

Constraints

  • 1 ≤ score.length ≤ 104
  • 0 ≤ score[i] ≤ 106
  • All values in score are unique

Visualization

Tap to expand
Relative Ranks - Optimal Solution INPUT score array (n=5) index: 0 1 2 3 4 5 4 3 2 1 After Sorting (desc): 5 4 3 2 1 1st 2nd 3rd 4th 5th All scores are unique Highest score = Rank 1 Score to Original Index Map 5-->0, 4-->1, 3-->2, 2-->3, 1-->4 ALGORITHM STEPS 1 Create Index Pairs Store (score, index) pairs [(5,0),(4,1),(3,2),(2,3),(1,4)] 2 Sort by Score (desc) Highest scores first [(5,0),(4,1),(3,2),(2,3),(1,4)] 3 Assign Ranks Position 0,1,2 = Medals Gold Silver Bronze 4,5... 4 Map Back to Original Use stored indices idx 0 --> Gold idx 1 --> Silver idx 2 --> Bronze idx 3 --> "4" idx 4 --> "5" O(n log n) Time FINAL RESULT answer array index: 0 1 2 3 4 Gold Medal Silver Medal Bronze Medal "4" "5" Output: ["Gold Medal","Silver Medal", "Bronze Medal","4","5"] Verification: score[0]=5 (highest) --> Gold score[1]=4 (2nd) --> Silver score[2]=3 (3rd) --> Bronze score[3]=2 (4th) --> "4" score[4]=1 (5th) --> "5" OK - Verified! Key Insight: The optimal approach uses sorting with index tracking. By storing original indices with scores, we can sort to determine ranks, then map results back to original positions. Time: O(n log n) for sorting. Alternative: Use a max-heap or hash map for score-to-rank mapping. TutorialsPoint - Relative Ranks | Optimal Solution (Sort + Index Mapping)
Asked in
Amazon 15 Google 8 Microsoft 6
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen