# Leetcode – Rank Transform of an Array Solution

Preparing for technical interviews often involves practicing coding problems, and platforms like Leetcode are indispensable resources for this preparation. One such intriguing problem on Leetcode is the “Rank Transform of an Array” problem. This problem tests your understanding of array manipulation, sorting, and mapping techniques. In this article, we will delve into the problem, its constraints, and various approaches to solve it, including their time and space complexities.

## Problem Statement

The problem can be summarized as follows:

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:

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

### Example:

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.

## Approaches to Solve the Problem

### Naive Approach

The naive approach would involve sorting the array and then mapping each element in the original array to its rank from the sorted array.

Here is how you can implement this in Python:

def arrayRankTransform(arr):
sorted_arr = sorted(set(arr))
rank_dict = {}

for rank, val in enumerate(sorted_arr, start=1):
rank_dict[val] = rank

return [rank_dict[val] for val in arr]

Time Complexity: O(N log N)
Space Complexity: O(N)

### Optimized Approach Using Sort

You can optimize the previous approach by combining the sort and mapping steps.

def arrayRankTransform(arr):
if not arr:
return []

arr_with_index = [(val, index) for index, val in enumerate(arr)]
arr_with_index.sort(key=lambda x: x)

rank = 1
prev_val = arr_with_index
arr_with_index = (rank, arr_with_index)

for i in range(1, len(arr_with_index)):
val, index = arr_with_index[i]
if val != prev_val:
rank += 1
arr_with_index[i] = (rank, index)
prev_val = val

arr_with_index.sort(key=lambda x: x)
return [rank for rank, _ in arr_with_index]

Time Complexity: O(N log N)
Space Complexity: O(N)

## Why the Optimized Approach Works

The optimized approach starts by enumerating the array’s elements alongside their indices, allowing us to maintain each element’s original position in the array. We then sort this array based on the elements’ values, making it easier to rank them.

After sorting, we iterate through the array to determine each element’s rank, considering that equal elements should have the same rank. Finally, we sort the array again based on the original indices, reconstructing the array in its original order but with the ranks replacing the elements.

## Edge Cases and Testing

1. Empty array: An empty array should return an empty array.
2. Array with a single element: Should return  since it’s the only element, making its rank 1.
3. Array with duplicate elements: The ranks of the duplicate elements should be the same.