human
Find relative rank of each element in array | Function to find relative rank for each element in the array A [ ].
Create Rank Array.
Stack to store numbers in non - decreasing order from right.
Push last element in stack.
Iterate from second last element to first element.
If current element is less than the top of stack and append A [ i ] in stack.
Rank is stack size - 1 for current element.
Pop elements from stack till current element is greater than the top.
Push current element in Stack.
Rank is stack size - 1.
Print rank of all elements.
Driver Code.
Given array A [ ].
Function call"
Solution in Python:
assistant
#Here is code to find relative rank of each element in array and the other steps required.
 def findRank ( A , N ) :
	rank = [ 0 ] * N
	s = [ ]
	s . append ( A [ N - 1 ] )
	for i in range ( N - 2 , - 1 , - 1 ) :
		if ( A [ i ] < s [ - 1 ] ) :
			s . append ( A [ i ] )
			rank [ i ] = len ( s ) - 1
		else :
			while ( len ( s ) > 0 and A [ i ] >= s [ - 1 ] ) :
				del s [ - 1 ]
			s . append ( A [ i ] )
			rank [ i ] = len ( s ) - 1
	for i in range ( N ) :
		print ( rank [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	A = [ 1 , 2 , 3 , 5 , 4 ]
	N = len ( A )
	findRank ( A , N )