FastPrepArray Rank Transform

Array Rank Transform

Tata Consultancy Services logoTata Consultancy Services● EasyOA
Learn

Problem statement

You are given an integer array arr. Replace each value with its rank after sorting all distinct values in increasing order.

The smallest distinct value has rank 1. Equal values must receive the same rank, and ranks must be consecutive with no gaps.

Return the transformed array in the original order.

Function

arrayRankTransform(arr: int[]) → int[]

Examples

Example 1

arr = [40,10,20,30]return = [4,1,2,3]

The sorted distinct values are 10,20,30,40, so their ranks are 1,2,3,4.

Example 2

arr = [100,100,100]return = [1,1,1]

All values are equal, so all positions receive the same smallest rank.

More Tata Consultancy Services problems

See Tata Consultancy Services hiring insights
public int[] arrayRankTransform(int[] arr) {
  // write your code here
}
arr[40,10,20,30]
expected[4,1,2,3]
Checking account…