Relative Ranks
Rank athletes by score and label the top three with medals, keeping output in the original order.
Open on LeetCode ↗Intuition
The trap is sorting the scores array directly and losing track of which athlete each score belonged to. The answer has to be written back into the ORIGINAL position of each athlete, so the original index has to travel with the score through the sort, not get discarded by it. Pack each score into a pair with its starting index before sorting, sort the pairs by score, then walk the sorted pairs and write the medal or rank string to answer[originalIndex]. The scores array itself is never reordered in place; only a parallel structure carrying both pieces of information is.
Approach
Pair every score with its original index
Build a list of (score, index) pairs in one pass over the input. This is the step that prevents the trap: once the score is bundled with where it came from, no later sort can lose that information.
Sort the pairs by score descending
Sort the pairs, not the raw scores, using score as the key. The pair carrying the highest score ends up first, and its original index is still attached.
Write results back by original index
Walk the sorted pairs in rank order, assigning 'Gold Medal', 'Silver Medal', 'Bronze Medal' to the first three and the numeric rank as a string to the rest, writing each result into answer[pair.index] rather than into the current loop position.
Solution & live demo
Edge cases
the lone score gets 'Gold Medal' regardless of its value
only as many medal labels are assigned as there are athletes; no bronze if there are only two
the sort is a no-op but pairing with indices still happens the same way
ranking depends only on relative order, not on the numeric gaps between scores