Replace each element by its rank in the given array | faceprep

Replace each element by its rank in the given array | faceprep

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.



Replacing elements by its rank in the given array



For example, consider the array

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




Replacing elements by its rank in the given array


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

@@coding::1@@




Recommended Programs



Replacing elements by its rank in the given array


c