Replace each element of Array with it’s corresponding rank
Last Updated :
18 May, 2021
Given an array arr[] of N integers, the task is to replace each element of Array with their rank in array.
The rank of an element is defined as the distance between the element with the first element of the array when the array is arranged in ascending order. If two or more are same in the array then their rank is also the same as the rank of the first occurrence of the element.
For Example: Let the given array arr[] = {2, 2, 1, 6}, then rank of elements is given by:
sorted array is:
arr[] = {1, 2, 2, 6}
Rank(1) = 1 (at index 0)
Rank(2) = 2 (at index 1)
Rank(2) = 2 (at index 2)
Rank(6) = 4 (at index 3)
Examples:
Input: arr[] = [100, 5, 70, 2]
Output: [4, 2, 3, 1]
Explanation:
Rank of 2 is 1, 5 is 2, 70 is 3 and 100 is 4.
Input: arr[] = [100, 2, 70, 2]
Output: [3, 1, 2, 1]
Explanation:
Rank of 2 is 1, 70 is 2 and 100 is 3.
Naive Approach: The naive approach is to find the rank of each element is 1 + the count of smaller elements in the array for the current element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above naive approach find ranks of elements and then assign the rank to the elements. Below are the steps:
- To compute the rank of the element first make a copy of given arr[] then sort that copied array in ascending order.
- Then traverse in the copied array and put their rank in HashMap by taking a rank variable.
- If the element is already present in HashMap then don’t update rank otherwise update rank of the element in HashMap and increment rank variable as well.
- Traverse the given array arr[] assign the rank of each element using the rank stored in HashMap.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void changeArr( int input[], int N)
{
int newArray[N];
copy(input, input + N, newArray);
sort(newArray, newArray + N);
int i;
map< int , int > ranks;
int rank = 1;
for ( int index = 0; index < N; index++)
{
int element = newArray[index];
if (ranks[element] == 0)
{
ranks[element] = rank;
rank++;
}
}
for ( int index = 0; index < N; index++)
{
int element = input[index];
input[index] = ranks[input[index]];
}
}
int main()
{
int arr[] = { 100, 2, 70, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
changeArr(arr, N);
cout << "[" ;
for ( int i = 0; i < N - 1; i++)
{
cout << arr[i] << ", " ;
}
cout << arr[N - 1] << "]" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static void changeArr( int [] input)
{
int newArray[]
= Arrays
.copyOfRange(input,
0 ,
input.length);
Arrays.sort(newArray);
int i;
Map<Integer, Integer> ranks
= new HashMap<>();
int rank = 1 ;
for ( int index = 0 ;
index < newArray.length;
index++) {
int element = newArray[index];
if (ranks.get(element) == null ) {
ranks.put(element, rank);
rank++;
}
}
for ( int index = 0 ;
index < input.length;
index++) {
int element = input[index];
input[index]
= ranks.get(input[index]);
}
}
public static void main(String[] args)
{
int [] arr = { 100 , 2 , 70 , 2 };
changeArr(arr);
System
.out
.println(Arrays
.toString(arr));
}
}
|
Python3
def changeArr(input1):
newArray = input1.copy()
newArray.sort()
ranks = {}
rank = 1
for index in range ( len (newArray)):
element = newArray[index];
if element not in ranks:
ranks[element] = rank
rank + = 1
for index in range ( len (input1)):
element = input1[index]
input1[index] = ranks[input1[index]]
if __name__ = = "__main__" :
arr = [ 100 , 2 , 70 , 2 ]
changeArr(arr)
print (arr)
|
C#
using System;
using System.Collections.Generic;
using System.Text;
class GFG{
static void changeArr( int [] input)
{
int []newArray = new int [input.Length];
Array.Copy(input, newArray, input.Length);
Array.Sort(newArray);
Dictionary< int ,
int > ranks= new Dictionary< int ,
int >();
int rank = 1;
for ( int index = 0;
index < newArray.Length;
index++)
{
int element = newArray[index];
if (!ranks.ContainsKey(element))
{
ranks[element] = rank;
rank++;
}
}
for ( int index = 0;
index < input.Length;
index++)
{
input[index] = ranks[input[index]];
}
}
public static void Main( string [] args)
{
int [] arr = { 100, 2, 70, 2 };
changeArr(arr);
Console.WriteLine( "[{0}]" ,
string .Join( ", " , arr));
}
}
|
Javascript
<script>
function changeArr(input, N)
{
var newArray = JSON.parse(JSON.stringify(input));
newArray.sort((a,b)=> a-b);
var i;
var ranks = new Map();
var rank = 1;
for ( var index = 0; index < N; index++)
{
var element = newArray[index];
if (!ranks.has(element))
{
ranks.set(element, rank);
rank++;
}
}
for ( var index = 0; index < N; index++)
{
var element = input[index];
input[index] = ranks.get(input[index]);
}
return input;
}
var arr = [100, 2, 70, 2];
var N = arr.length;
arr = changeArr(arr, N);
document.write( "[" );
for ( var i = 0; i < N - 1; i++)
{
document.write( arr[i] + ", " );
}
document.write( arr[N - 1] + "]" );
</script>
|
[3, 1, 2, 1]
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...