Problem Statement

Given an integer array ARR of size N, replace each element of this array with its corresponding rank.

Explanation

The rank of an element is an integer between 1 and N (inclusive) that represents its size relative to other elements in the array. Here are the rules to determine the rank:

1. Ranks start from 1.
2. Larger elements receive larger ranks. Equal elements share the same rank.
3. Ranks should be assigned as small as possible.

Input

The first line contains an integer T, the number of test cases.
Following is 2*T lines describing the test cases:
For each test case, the first line contains an integer N (the number of elements in ARR).
The second line contains N space-separated integers, the elements of ARR.

Output

For each test case, print the elements of ARR separated by spaces, with each element replaced by its rank.

Example

Input:
2
5
50 30 20 40 10
3
100 200 100
Output:
5 3 2 4 1
2 3 2

Constraints

  • 1 <= T <= 50
  • 1 <= N <= 10^4
  • -10^9 <= ARR[i] <= 10^9
  • The array ARR[i] denotes the ith element of the array.
Note
You do not need to print anything; instead, return the integer array ARR with each element replaced by its rank.
AnswerBot
4mo

Replace elements in an array with their corresponding ranks based on given rules.

  • Iterate through the array and assign ranks based on the rules provided.

  • Handle equal elements by assigning them the same...read more

Help your peers!
Select
Add answer anonymously...

Groww Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Best Time To Buy and Sell Stock Problem Statement You are given an array 'PRICES...read more
A Software Developer Intern was asked Q. Stack with getMin Operation Create a stack data structure that supports not only...read more
A Software Developer Intern was asked Q. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Best Time To Buy and Sell Stock Problem Statement You are given an array 'PRICES...read more
A Software Developer Intern was asked Q2. Stack with getMin Operation Create a stack data structure that supports not only...read more
A Software Developer Intern was asked Q3. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more
Groww Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits