Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given an array with **N** distinct elements, convert the given array to a reduced form where all elements are in range from **0** to **N-1**. The order of elements is same, i.e., **0** is placed in place of smallest element, **1** is placed for second smallest element, … **N-1** is placed for largest element.

**Example 1:**

**Input:
**N = 3
Arr[] = {10, 40, 20}
**Output: **0 2 1
**Explanation:** 10 is the least element so it
is replaced by 0. 40 is the largest
element so it is replaced by 3-1 = 2. And
20 is the 2nd smallest element so it is
replaced by 1.

**Example 2:**

**Input:
**N = 5
Arr[] = {5, 10, 40, 30, 20}
**Output:** 0 1 4 3 2
**Explanation:** As 5 is smallest element,
it's replaced by 0. Then 10 is 2nd
smallest element, so it's replaced by 1.
Then 20 is 3rd smallest element, so it's
replaced by 2. And so on.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **convert()** which takes the array of integers **arr **and **n **as parameters and returns an array of integers denoting the answer.

**Expected Time Complexity:** O(N*logN)

**Expected Auxiliary Space:** O(N)

**Constraints:**

1 ≤ N ≤ 10^{5}

1 ≤ Arr[i] ≤ 10^{6}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Convert an array to reduced form

...

Here is some troubleshooting advice:

- Reset the IDE using the second button on the top right corner.
- Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
- You can access the hints to get an idea about what is expected of you as well as the final solution code.
- You can view the solutions submitted by other users from the submission tab.

Here is some troubleshooting advice for contest problems

- Make sure you are not using ad-blockers.
- Disable browser extensions.
- We recommend using latest version of your browser for best experience.
- Avoid using static/global variables in coding problems as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases in coding problems does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.