Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Webinars
About Us

Edit
Reply




Edit

Replace each element by its rank in the given array | FACE Prep

Published on 10 Mar 2020

Program to replace each element by its rank in the given array is discussed here. Given an array of distinct integers, we need to replace each element of the array with its rank. The minimum value element will have the highest rank.


For example, consider the array

  • Input array = {10, 8, 15, 12, 6, 20, 1}
  • Output array = {4, 3, 6, 5, 2, 7, 1}

replace elements by its rank

Algorithm


A solution can be provided using maps. The array element and its index are stored in two columns in the map and later the index is replaced by the element's rank.


  • Elements in a map will be stored in sorted order, so on iterating it, the elements will be in an increasing order sequence.
  • Assign values for each element in increasing order starting from 1 to n, incrementing by 1 for each element.


Program to replace each element by its rank in the given array


C++
Java

Output
Input - Enter the number of elements : 7 Input the array elements : 10 8 15 12 6 20 1 Output - After ranking : 4 3 6 5 2 7 1


Time complexity: O(n log n)


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c plus plus'
Articles Practice Exercises