Array Rank Transform
Updated
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 Online Assessment questions
Similar questions at other companies
- Build the Task HierarchyPendo · OA
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
- Items SortAkuna Capital · OAAkuna Capital interview questions
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Find Max Health SumAmazon · OA
- Influencers SquadTikTok · OATikTok interview questions
- Count Teams Outscoring a Target TeamBloomberg LP · OABloomberg LP interview questions