Given an of distinct integers, each of the by its corresponding in the .

The minimum element in the array has rank 1, the second minimum element has rank of 2 and so on.. For example,

Input:  { , 8, 15, , 6, , 1 }

Output: { 4, 3, 6, 5, 2, 7, 1 }

The idea is to store each element’s index in an ordered map (Since the array contains all distinct integers, we can use array elements and their index as key-value pair in the map). Since elements are stored in sorted order in ordered map, if we iterate through the map, we get elements in increasing order. So, for each element in increasing order, we start assigning values to it starting from number 1 till n.

Below is C++ and Java implementation of the idea:

[tabby title=”C++”]

```#include
#include
using namespace std;

// Function to replace each element of the array by its rank in the array
void transform(int arr[], int n)
{
// create an empty ordered map
map map;

// store (element, index) pair in the map
for (int i = 0; i
Output:4 3 6 5 2 7 1
[tabby title="Java"]
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

class Transform
{
// Function to replace each element of the array by its rank in the array
public static void transform(int[] arr)
{
// create an empty TreeMap
Map map = new TreeMap();

// store (element, index) pair in TreeMap
for (int i = 0; i  entry : map.entrySet()) {
arr[entry.getValue()] = rank++;
}
}

// Find all distinct combinations of given length
public static void main(String[] args)
{
int[] A = { 10, 8, 15, 12, 6, 20, 1 };

// transform the array
transform(A);

// print the transformed array
System.out.println(Arrays.toString(A));
}
}