Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

506. Relative Ranks

Easy72.6% Acceptance
ArraySortingHeap (Priority Queue)
Asked by:
G
Google
ProblemSolutions (12)VideosCompanies (2)Notes

Problem Statement

You are given an integer array score of size n, where score[i] is the score of the ith 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 xth place athlete's rank is "x").

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

Example 1:

Input: score = [5,4,3,2,1]
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].

Example 2:

Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].

Constraints:

  • n == score.length
  • 1 <= n <= 104
  • 0 <= score[i] <= 106
  • All the values in score are unique.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Accenture

Approach

In #506 Relative Ranks, you are given an array representing athletes' scores and must assign ranks based on their performance. The highest score gets "Gold Medal", the second gets "Silver Medal", the third gets "Bronze Medal", and the remaining athletes receive their numeric rank.

A common approach is to sort the scores while keeping track of their original indices. After sorting in descending order, iterate through the sorted list and assign the correct medal or rank string based on the position. Then place the result back into the output array using the stored indices.

Another approach uses a max heap (priority queue) to repeatedly extract the highest score and assign ranks in order. This also ensures the highest-performing athletes are processed first.

The sorting method is usually simpler and widely used in interviews. Its time complexity is dominated by sorting, while additional space is used to store indices and results.

Complexity

ApproachTime ComplexitySpace Complexity
Sorting with index trackingO(n log n)O(n)
Max Heap (Priority Queue)O(n log n)O(n)

Video Solution Available

code Explainer

View all video solutions

Solutions (12)

Sorting with Index Mapping

The idea is to sort the scores but preserve their original indices by pairing each score with its index. Once sorted, we can easily determine their ranks by iterating over the sorted list. We then assign the corresponding rank values based on their positions (i.e., 'Gold Medal' for the first position, etc.). This approach utilizes additional space to maintain the original indices while sorting the scores.

Time Complexity: O(n log n), where n is the number of scores, for the sorting operation.
Space Complexity: O(n) for storing the pair struct array and the result array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5struct ScoreIndex {
6    int score

    
    
    

Explanation

The solution involves creating a pair structure (ScoreIndex) to hold both the score and its original index. We make use of qsort to sort the array in descending order based on scores. After sorting, the rank is assigned according to the sorted position, converting the top three into medal strings and others into strings of their positions. Finally, the ranks are filled into the result array based on original indices.

HashMap to Determine Ranks

In this approach, we employ a hash map (or dictionary) to map each score to its ranking position in a sorted list. The scores are first sorted to determine order-based ranks. We then iterate through original scores, using the map to quickly assign the appropriate ranking (medal or numeric) to each score.

Time Complexity: O(n log n) from sorting.
Space Complexity: O(n) due to storage of the sorted copy.

CC++JavaPythonC#JavaScript
1#





Video Solutions

Watch expert explanations and walkthroughs

506. Relative Ranks | LEETCODE EASY

code Explainer
6:483,482 views

Asked By Companies

2 companies
G
Google
A
Accenture

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

3SumMedium
3Sum ClosestMedium
4SumMedium
Group AnagramsMedium
More similar problems

Related Topics

ArraySortingHeap (Priority Queue)

Problem Stats

Acceptance Rate72.6%
DifficultyEasy
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Relative Ranks asked in coding interviews?

Yes, Relative Ranks is a common easy-level interview problem used to test understanding of sorting, heaps, and array manipulation. It often appears in coding practice platforms and interview preparation lists.

What data structure is best for Relative Ranks?

Arrays combined with sorting are typically the best choice for this problem. Alternatively, a max heap (priority queue) can be used to repeatedly extract the highest score and assign ranks in order.

What is the optimal approach for Relative Ranks?

The most common approach is to sort the scores in descending order while keeping track of their original indices. After sorting, assign medals to the top three and numeric ranks to the rest. This method is efficient and easy to implement with O(n log n) time complexity.

Why do we store original indices in Relative Ranks?

Sorting changes the order of elements, but the final ranks must correspond to the athletes' original positions. Storing indices allows you to map the computed rank back to the correct position in the result array.

;
7
int
index
;
8
}
;
9
10
int
compare
(
const
void
*
a
,
const
void
*
b
)
{
11
return
(
(
struct
ScoreIndex
*
)
b
)
->
score
-
(
(
struct
ScoreIndex
*
)
a
)
->
score
;
12
}
13
14
char
*
*
findRelativeRanks
(
int
*
score
,
int
scoreSize
,
int
*
returnSize
)
{
15
struct
ScoreIndex
scoreIndex
[
scoreSize
]
;
16
char
*
*
result
=
(
char
*
*
)
malloc
(
scoreSize
*
sizeof
(
char
*
)
)
;
17
const
char
*
medals
[
]
=
{
"Gold Medal"
,
"Silver Medal"
,
"Bronze Medal"
}
;
18
19
for
(
int
i
=
0
;
i
<
scoreSize
;
i
++
)
{
20
scoreIndex
[
i
]
.
score
=
score
[
i
]
;
21
scoreIndex
[
i
]
.
index
=
i
;
22
}
23
24
qsort
(
scoreIndex
,
scoreSize
,
sizeof
(
struct
ScoreIndex
)
,
compare
)
;
25
26
for
(
int
i
=
0
;
i
<
scoreSize
;
i
++
)
{
27
if
(
i
<
3
)
{
28
result
[
scoreIndex
[
i
]
.
index
]
=
strdup
(
medals
[
i
]
)
;
29
}
else
{
30
result
[
scoreIndex
[
i
]
.
index
]
=
(
char
*
)
malloc
(
10
*
sizeof
(
char
)
)
;
31
sprintf
(
result
[
scoreIndex
[
i
]
.
index
]
,
"%d"
,
i
+
1
)
;
32
}
33
}
34
35
*
returnSize
=
scoreSize
;
36
return
result
;
37
}
38
include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<string.h>
4
5
int
cmpfunc
(
const
void
*
a
,
const
void
*
b
)
{
6
return
(
*
(
int
*
)
b
-
*
(
int
*
)
a
)
;
7
}
8
9
char
*
*
findRelativeRanks
(
int
*
score
,
int
scoreSize
,
int
*
returnSize
)
{
10
int
*
sorted
=
(
int
*
)
malloc
(
scoreSize
*
sizeof
(
int
)
)
;
11
memcpy
(
sorted
,
score
,
scoreSize
*
sizeof
(
int
)
)
;
12
13
qsort
(
sorted
,
scoreSize
,
sizeof
(
int
)
,
cmpfunc
)
;
14
15
char
*
*
result
=
(
char
*
*
)
malloc
(
scoreSize
*
sizeof
(
char
*
)
)
;
16
const
char
*
medals
[
]
=
{
"Gold Medal"
,
"Silver Medal"
,
"Bronze Medal"
}
;
17
18
for
(
int
i
=
0
;
i
<
scoreSize
;
i
++
)
{
19
int
rank
=
(
int
)
(
strchr
(
sorted
,
score
[
i
]
)
-
sorted
)
;
20
if
(
rank
<
3
)
{
21
result
[
i
]
=
strdup
(
medals
[
rank
]
)
;
22
}
else
{
23
result
[
i
]
=
(
char
*
)
malloc
(
10
*
sizeof
(
char
)
)
;
24
sprintf
(
result
[
i
]
,
"%d"
,
rank
+
1
)
;
25
}
26
}
27
28
free
(
sorted
)
;
29
*
returnSize
=
scoreSize
;
30
return
result
;
31
}
32

Explanation

This C solution sorts a copy of the original list and uses the sorted positions to determine ranks. The qsort function provides a sorted view, and scores are ranked by finding their positions in this sorted list. Medals are assigned for the top three ranks using conditional checks.