Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6454 |

@blackshadows | 6380 |

@mb1973 | 5718 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3709 |

@mantu_singh | 3556 |

@marius_valentin_dragoi | 3523 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1285 |

@gurshehzadsingh | 1052 |

@pritsahkar2000 | 812 |

@satyamchhatrala | 808 |

@nithinreddy3210 | 801 |

@iib2019024 | 745 |

@mohanreddy8847 | 704 |

@iamchaitanyahegde | 662 |

@shafeeq3543 | 651 |

@workit | 646 |

@AkashLahoty | 642 |

Complete Leaderboard |

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}

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

...