Rank of all elements in a Stream in descending order when they arrive
Given a stream of numbers as arr, the task is to find the rank of each element in the stream in descending order when they arrive.
Rank is defined as the total number of elements that is greater than the arriving element, where rank 1 defines the maximum value in the stream. Each value in the array is unique.
Examples:
Input: arr = [88, 14, 69, 30, 29, 89]
Output: 1 2 2 3 4 1
Explanation:
First 88 arrives, so its rank is 1.
when 14 arrives, 14 is less than 88 so its rank is 2.
when 69 arrives, 69 is less than 88 and greater than 14 so its rank is 2.
when 30 arrives, 30 is less than 88 and 69 so its rank is 3.
when 29 arrives, 29 is less than 88, 69, 30 than 29 so its rank is 4.
when 89 arrives, 89 is greater than all values so its rank is 1.
The rank of elements of array 1 2 2 3 4 1Input: arr = [100, 110, 80, 85, 88, 89]
Output: 1 1 3 3 3 3
Explanation:
First 100 arrive so its rank is 1.
when 110 arrive, 110 is greater than 100 so its rank is 1.
when 80 arrive, 80 is less than 110 and 100 so its rank is 3.
when 85 arrive, 85 is less than 110 and 100 so its rank is 3.
when 88 arrive, 88 is less than 110 and 100 so its rank is 3.
when 89 arrive, 89 is less than 110 and 100 so its rank is 3.
The rank of elements of array 1 1 3 3 3 3
Naive Approach:
 The first element of the array will always have rank 1.
 Iterate over the array, if the element is greatest among previous elements then its rank is 1.

If the element is not greatest than previous compared elements. then the rank of this element will be the number of greater elements before it.
For example, lets say if it is greater than 2 previous elements so its rank is 3.
Below is the implementation of the above approach:
C++
// C++ program to rank of all elements // in a Stream in descending order // when they arrive #include <iostream> using namespace std; // FindRank function to find rank void FindRank( int arr[], int length) { // Rank of first element is always 1 cout << "1" << " " ; // Iterate over array for ( int i = 1; i < length; i++) { // As element let say its rank is 1 int rank = 1; // Element is compared // with previous elements for ( int j = 0; j < i; j++) { // If greater than previous // than rank is incremented if (arr[j] > arr[i]) rank++; } // print rank cout << rank << " " ; } } // Driver code int main() { // array named arr int arr[] = {88, 14, 69, 30, 29, 89}; // length of arr int len = sizeof (arr)/ sizeof (arr[0]); FindRank(arr, len); return 0; } // This code is contributed by AnkitRai01 
Java
// Java program to rank of all elements // in a Stream in descending order // when they arrive import java.util.*; class GFG{ // FindRank function to find rank static void FindRank( int arr[], int length) { // Rank of first element is always 1 System.out.print( "1" + " " ); // Iterate over array for ( int i = 1 ; i < arr.length; i++) { // As element let say its rank is 1 int rank = 1 ; // Element is compared // with previous elements for ( int j = 0 ; j < i; j++) { // If greater than previous // than rank is incremented if (arr[j] > arr[i]) rank++; } // print rank System.out.print(rank + " " ); } } // Driver code public static void main(String args[]){ // array named arr int arr[] = { 88 , 14 , 69 , 30 , 29 , 89 }; // length of arr int len = arr.length; FindRank(arr, len); } } // This code is contributed by AbhiThakur 
Python3
# Python program to rank of all elements # in a Stream in descending order # when they arrive # FindRank function to find rank def FindRank(arr, length): # Rank of first element is always 1 print ( 1 , end = " " ) # Iterate over array for i in range ( 1 , length): # As element let say its rank is 1 rank = 1 # Element is compared # with previous elements for j in range ( 0 , i): # If greater than previous # than rank is incremented if (arr[j] > arr[i]): rank = rank + 1 # print rank print (rank, end = " " ) # Driver code if __name__ = = '__main__' : # array named arr arr = [ 88 , 14 , 69 , 30 , 29 , 89 ] # length of arr length = len (arr) FindRank(arr, length) 
C#
// C# program to rank of all elements // in a Stream in descending order // when they arrive using System; class GFG{ // FindRank function to find rank static void FindRank( int [] arr, int length) { // Rank of first element is always 1 Console.Write( "1" + " " ); // Iterate over array for ( int i = 1; i < arr.Length; i++) { // As element let say its rank is 1 int rank = 1; // Element is compared // with previous elements for ( int j = 0; j < i; j++) { // If greater than previous // than rank is incremented if (arr[j] > arr[i]) rank++; } // print rank Console.Write(rank + " " ); } } // Driver code public static void Main(){ // array named arr int [] arr = {88, 14, 69, 30, 29, 89}; // length of arr int len = arr.Length; FindRank(arr, len); } } // This code is contributed by AbhiThakur 
1 2 2 3 4 1
Time complexity: O(N^{2}), where N is length of array.
Space complexity: O(1)
Efficient Approach: The idea is to use Binary search to find the rank of the number. It will give the count of numbers which are greater than the given number.
Below is the implementation of the above approach:
Python3
# Python program to rank of all elements # in a Stream in descending order # when they arrive # import of bisect to use bisect.insort() import bisect # FindRank function to find rank def FindRank(arr, length): # rank list to store values of # array in ascending order rank = [] first = arr[ 0 ] rank.append(first) # Rank of first element is always 1 print ( 1 , end = " " ) # Iterate over remaining array for i in range ( 1 , length): val = arr[i] # element inserted in the rank list # using the binary method bisect.insort(rank, val) # To find rank of that element # length of rank array  index of element # in rank array eleRank = len (rank)  rank.index(val) # print of rank of element of array print (eleRank, end = " " ) # Driver code if __name__ = = '__main__' : # array named arr arr = [ 88 , 14 , 69 , 30 , 29 , 89 ] # lenght of arr length = len (arr) FindRank(arr, length) 
1 2 2 3 4 1
Time complexity: O(N * log N), where N is length of array.
Space complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a studentfriendly price.
Recommended Posts:
 Rearrange array such that difference of adjacent elements is in descending order
 Sorting rows of matrix in ascending order followed by columns in descending order
 Sorting rows of matrix in descending order followed by columns in ascending order
 Rank of an element in a stream
 Sort first k values in ascending order and remaining nk values in descending order
 Stable sort for descending order
 Program to sort string in descending order
 Sorting 2D Vector in C++  Set 2 (In descending order by row and column)
 Print numbers in descending order along with their frequencies
 Sorting Vector of Pairs in C++  Set 2 (Sort in descending order by first and second)
 Sort prime numbers of an array in descending order
 Sort an array in descending order based on the sum of its occurrence
 Sort Matrix in alternating ascending and descending order rowwise
 Rank of all elements in an array
 Sort all even numbers in ascending order and then sort all odd numbers in descending order
 Sort first half in ascending and second half in descending order  1
 Sort first half in ascending and second half in descending order  Set 2
 Sort elements of an array in increasing order of absolute difference of adjacent elements
 Sort even and odd placed elements in increasing order
 Permute the elements of an array following given order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.