Skip to main content

Rank Transform of an Array - Solution & Explanation

EasyArrayHash TableSorting12 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

Given an array of integers arr, replace each element with its rank.

The rank represents how large the element is. The rank has the following rules:

  • Rank is an integer starting from 1.
  • The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
  • Rank should be as small as possible.

 

Example 1:

Input: arr = [40,10,20,30]
Output: [4,1,2,3]
Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.

Example 2:

Input: arr = [100,100,100]
Output: [1,1,1]
Explanation: Same elements share the same rank.

Example 3:

Input: arr = [37,12,28,9,100,56,80,5,12]
Output: [5,3,4,2,8,6,7,1,3]

 

Constraints:

  • 0 <= arr.length <= 105
  • -109 <= arr[i] <= 109

Approach Overview

Problem Overview: Given an integer array, replace every element with its rank after sorting the values in ascending order. The smallest value gets rank 1. Equal values share the same rank, and ranks increase only when the value changes.

Approach 1: Sorting and Ranking (O(n log n) time, O(n) space)

This approach sorts the unique values and assigns ranks incrementally. Start by copying the array and sorting it. Then iterate through the sorted list and build a hash table that maps each number to its rank. Only assign a new rank when the current value differs from the previous value so duplicates receive the same rank. Finally, iterate through the original array and replace each value with its stored rank using constant-time hash lookups.

The key insight is separating ordering from mapping. Sorting determines the rank order, while the hash map allows quick translation back to the original positions. This approach works well for most cases and keeps the implementation simple. Time complexity comes from sorting the array (O(n log n)) and the additional passes are linear.

Approach 2: Using Coordinate Compression (O(n log n) time, O(n) space)

Coordinate compression reduces large or scattered values into a compact rank range. First, copy the array and sort it. Remove duplicates from the sorted list so you only keep unique values. Each unique value’s index in this sorted list determines its rank (index + 1). Store these mappings in a dictionary and then transform the original array by replacing each element with its compressed coordinate (rank).

This method is conceptually similar to the first approach but emphasizes the compression idea commonly used in competitive programming and sorting problems. It is especially useful when the input values are very large or sparse but you only care about relative ordering. By compressing them into ranks, you reduce the value domain without losing ordering information.

Both methods rely on sorting to establish global ordering and use a array traversal to rebuild the result. The difference is mainly conceptual: the first focuses on ranking while the second frames the process as coordinate compression.

Recommended for interviews: The sorting + hash map solution is the most direct and the one interviewers typically expect. It clearly demonstrates understanding of ordering, duplicate handling, and efficient lookups. Mentioning coordinate compression shows deeper algorithmic awareness, especially in problems involving large ranges or index remapping.

Approach 1: Sorting and Ranking

This approach involves sorting the array to determine the rank of each element. After sorting, unique elements are mapped to their ranks.

The C solution involves sorting the array and creating a map of ranks. Sorted unique elements are assigned ranks incrementally. The original elements are then mapped to their ranks using this mapping.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N log N) due to sorting.
Space Complexity: O(N) for storing the sorted array and rank map.

Try this approach in the editor β†’

Approach 2: Using Coordinate Compression

Coordinate compression is a method to map large ranges of numbers to smaller ranges, maintaining their relative order. This approach uses this idea to assign ranks.

The C coordinate compression solution involves sorting the array and creating a rank map by compressing coordinates. This map is used for rank assignment efficiently.

Code

C

C++

Python

Complexity

Time Complexity: O(N log N) due to sorting and binary search operations.
Space Complexity: O(N) for rank maps.

Try this approach in the editor β†’

Approach 3: Discretization

First, we copy an array t, then sort and deduplicate it to obtain an array of length m that is strictly monotonically increasing.

Next, we traverse the original array arr. For each element x in the array, we use binary search to find the position of x in t. The position plus one is the rank of x.

The time complexity is O(n times log n), and the space complexity is O(n). Here, n is the length of the array arr.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor β†’

Approach 4: Sorting + Hash Map

Code

TypeScript

JavaScript

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Sorting and Ranking

Time Complexity: O(N log N) due to sorting.
Space Complexity: O(N) for storing the sorted array and rank map.

Using Coordinate Compression

Time Complexity: O(N log N) due to sorting and binary search operations.
Space Complexity: O(N) for rank maps.

Discretizationβ€”
Sorting + Hash Mapβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting and Ranking with Hash MapO(n log n)O(n)General case. Clear implementation and easy to explain during interviews.
Coordinate CompressionO(n log n)O(n)Useful when values are large or sparse and you want to remap them into a compact rank range.

Video Solution

Rank Transform of an Array | Leetcode 1331 β€’ Techdose β€’ 7,777 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Rank Transform of an Array easy or hard?
Rank Transform of an Array is categorized as an Easy problem on LeetCode with a high acceptance rate around 70%. The challenge mainly involves handling duplicates correctly while assigning ranks after sorting. Understanding hash maps and sorting is sufficient to solve it.
How to solve Rank Transform of an Array in O(n)?
A true O(n) solution is generally not possible because the algorithm needs global ordering of elements, which requires sorting. Without constraints on the value range, comparison-based sorting leads to O(n log n) time. If the value range were very small, counting sort could theoretically reduce the complexity closer to O(n).
Rank Transform of an Array Python or Java solution?
In Python or Java, the standard solution sorts a copy of the array, builds a dictionary or HashMap mapping values to ranks, and then replaces each original value with its rank. The approach works the same across languages and maintains O(n log n) time complexity.
What is the best approach for Rank Transform of an Array?
The most common solution sorts the array values and assigns ranks using a hash map. After sorting, each unique number receives an increasing rank, and duplicates share the same rank. The original array is then transformed using constant-time lookups. This approach runs in O(n log n) time and O(n) space.
What data structure is used in Rank Transform of an Array?
The typical implementation uses a hash table to map each value to its rank after sorting. Arrays are used for iteration and reconstruction of the final result. Sorting algorithms provide the ordering needed to assign correct ranks.
What is the time complexity of Rank Transform of an Array?
The optimal solution runs in O(n log n) time due to the sorting step. After sorting, assigning ranks and reconstructing the result both take O(n). Space complexity is O(n) because a hash map or auxiliary array is used to store value-to-rank mappings.
Is Rank Transform of an Array asked at Google, Amazon, or Meta?
Variants of ranking and coordinate compression problems appear in interviews at companies like Amazon and Google. The question tests understanding of sorting, handling duplicates, and mapping values efficiently using hash tables. It is commonly used as an easy-to-medium warm-up problem.

Ready to solve this problem?

Practice Rank Transform of an Array with our built-in code editor and test cases.

Practice on FleetCode