VOOZH about

URL: https://www.geeksforgeeks.org/dsa/replace-each-element-of-array-with-its-corresponding-rank/

⇱ Replace each element of Array with it's corresponding rank - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace each element of Array with it's corresponding rank

Last Updated : 10 Jun, 2026

Given an array arr[] of n integers, convert it into its reduced form. In the reduced form, each element is replaced by its rank. The smallest element should be replaced with 0, the second smallest with 1, and so on, until the largest element is replaced with n - 1.

  • The relative positions of the elements in the array must remain unchanged.
  • For repeating elements, the element appearing earlier in the original array must be of smaller rank than the one appearing later.
  • You need to modify the array in-place and do not return anything.

Examples:

Input: arr[] = [10, 40, 20]
Output: [0, 2, 1]
Explanation: The elements in sorted order are [10, 20, 40]. Therefore, 10, 20, and 40 are assigned ranks 0, 1, and 2 respectively.

Input: arr[] = [0, 2, 1]
Output: [0, 2, 1]
Explanation: The array is already in reduced form.The elements 0, 1, and 2 are the smallest, second smallest, and largest elements respectively.

Input: arr[] = [1, 5, 3, 4, 3]
Output: [0, 4, 1, 3, 2]
Explanation: The elements of the array in sorted order are [1, 3, 3, 4, 5]. Assigning ranks from 0 to n - 1 gives 1 as rank 0, the first 3 as rank 1, the second 3 as rank 2, 4 as rank 3 and 5 as rank 4.

[Naive Approach] - Using Brute Force Method - O(n^2) time and O(n) space

The idea is to determine the rank of every element independently. For a given element, its rank is equal to the number of elements that are smaller than it. Since equal elements must receive different ranks based on their original order, we also count the equal elements that appear before the current element. The resulting count is the rank of that element.


Output
0 2 1 

[Efficient Approach] - Using Stable Sorting - O(n * log n) time and O(n) space

The idea is to first store each element along with its original index as a pair (value, index). Then, we perform a stable sort based on the values. After sorting, the position of each pair in the sorted array represents its rank. Finally, using the stored indices, we place these ranks back into their original positions in the array. Stable sorting ensures that equal elements retain their relative order, so an earlier occurrence receives a smaller rank than a later occurrence.

For the array arr[] = [1, 5, 3, 4, 3]

Step 1: Convert each element into a pair (value, index): [(1, 0), (5, 1), (3, 2), (4, 3), (3, 4)]

Step 2: Perform a stable sort based on the values: [(1, 0), (3, 2), (3, 4), (4, 3), (5, 1)]

Step 3: Assign ranks according to the position in the sorted array:

Step 3: Assign ranks, We start rank from 1:

  • (1, 0) -> rank 0
  • (3, 2) -> rank 1
  • (3, 4) -> rank 2
  • (4, 3) -> rank 3
  • (5, 1) -> rank 4

Step 4: Place the ranks back using the original indices:

  • index 0 -> 0
  • index 1 -> 4
  • index 2 -> 1
  • index 3 -> 3
  • index 4 -> 2

Step 5: The final reduced form of the array is: [0, 4, 1, 3, 2]


Output
0 2 1 
Comment
Article Tags: