Rank of all elements in a Stream in descending order when they arrive
Last Updated :
12 Jul, 2025
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 1
Input: 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
JavaScript
<script>
// javascript program to rank of all elements
// in a Stream in descending order
// when they arrive
// FindRank function to find rank
function FindRank(arr , length) {
// Rank of first element is always 1
document.write("1" + " ");
// Iterate over array
for (i = 1; i < arr.length; i++) {
// As element let say its rank is 1
var rank = 1;
// Element is compared
// with previous elements
for (j = 0; j < i; j++) {
// If greater than previous
// than rank is incremented
if (arr[j] > arr[i])
rank++;
}
// print rank
document.write(rank + " ");
}
}
// Driver code
// array named arr
var arr = [ 88, 14, 69, 30, 29, 89 ];
// length of arr
var len = arr.length;
FindRank(arr, len);
// This code contributed by umadevi9616
</script>
Time complexity: O(N2), 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:
C++
#include <bits/stdc++.h>
using namespace std;
void FindRank(int arr[], int length)
{
// rank list to store values of
// array in ascending order
vector<int> rank;
int first = arr[0];
rank.push_back(first);
// Rank of first element is always 1
cout << 1 << " ";
// Iterate over remaining array
for (int i = 1; i < length; i++)
{
int val = arr[i];
// element inserted in the rank list
// using the binary method
int ind = lower_bound(rank.begin(), rank.end(), val) - rank.begin();
rank.insert(rank.begin() + ind, val);
// To find rank of that element
// length of rank array - index of element
// in rank array
int eleRank = rank.size() - ind;
// print of rank of element of array
cout << eleRank << " ";
}
}
int main()
{
int arr[] = {88, 14, 69, 30, 29, 89};
int length = sizeof(arr) / sizeof(arr[0]);
FindRank(arr, length);
}
// This code is contributed by akashish__
Java
/*package whatever //do not write package name here */
import java.util.*;
class GFG {
static void FindRank(int arr[], int length){
// rank list to store values of
// array in ascending order
List<Integer> rank = new ArrayList<>();
int first = arr[0];
rank.add(first);
// Rank of first element is always 1
System.out.print(1 + " ");
//Iterate over remaining array
for(int i=1;i<length;i++){
int val = arr[i];
// element inserted in the rank list
// using the binary method
int ind = Collections.binarySearch(rank,val);
if(ind<0){
ind = -ind-1;
}else{
ind++;
}
rank.add(ind,val);
// To find rank of that element
// length of rank array - index of element
// in rank array
int eleRank = rank.size()-rank.indexOf(val);
// print of rank of element of array
System.out.print(eleRank + " ");
}
}
public static void main (String[] args)
{
int []arr = {88, 14, 69, 30, 29, 89};
int length = arr.length;
FindRank(arr, length);
}
}
// This code is contributed by aadityaburujwale.
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]
# length of arr
length = len(arr)
FindRank(arr, length)
C#
// Include namespace system
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
public class GFG
{
public static void FindRank(int[] arr, int length)
{
// rank list to store values of
// array in ascending order
var rank = new List<int>();
var first = arr[0];
rank.Add(first);
// Rank of first element is always 1
Console.Write(1.ToString() + " ");
// Iterate over remaining array
for (int i = 1; i < length; i++)
{
var val = arr[i];
// element inserted in the rank list
// using the binary method
var ind = rank.BinarySearch(val);
if (ind < 0)
{
ind = -ind - 1;
}
else
{
ind++;
}
rank.Insert(ind,val);
// To find rank of that element
// length of rank array - index of element
// in rank array
var eleRank = rank.Count - rank.IndexOf(val);
// print of rank of element of array
Console.Write(eleRank.ToString() + " ");
}
}
public static void Main(String[] args)
{
int[] arr = {88, 14, 69, 30, 29, 89};
var length = arr.Length;
GFG.FindRank(arr, length);
}
}
// This code is contributed by aadityaburujwale.
JavaScript
function lower_bound(arr, target){
for(let i = 0; i < arr.length; i++)
{
if(arr[i] >= target)
return i;
}
return arr.length;
}
function findRank(arr, length) {
// rank list to store values of
// array in ascending order
const rank = [];
const first = arr[0];
rank.push(first);
// Rank of first element is always 1
// console.log(1);
let ans = "1 ";
// Iterate over remaining array
for (let i = 1; i < length; i++) {
const val = arr[i];
// element inserted in the rank list
// using the binary method
const ind = lower_bound(rank, val);
rank.splice(ind, 0, val);
// To find rank of that element
// length of rank array - index of element
// in rank array
const eleRank = rank.length - ind;
// print of rank of element of array
// console.log(eleRank);
ans+=eleRank.toString()+" ";
}
console.log(ans);
}
const arr = [88, 14, 69, 30, 29, 89];
const length = arr.length;
findRank(arr, length);
// This code is contributed by akashish__
Time complexity: O(N * log N), where N is length of array.
Space complexity: O(N)
Similar Reads
Sort the linked list in the order of elements appearing in the array Given an array of size N and a Linked List where elements will be from the array but can also be duplicated, sort the linked list in the order, elements are appearing in the array. It may be assumed that the array covers all elements of the linked list.arr[] = list = Sorted list = Asked in Amazon Fi
9 min read
Print numbers in descending order along with their frequencies Given an array arr, the task is to print the elements of the array in descending order along with their frequencies.Examples:  Input: arr[] = {1, 3, 3, 3, 4, 4, 5} Output: 5 occurs 1 times 4 occurs 2 times 3 occurs 3 times 1 occurs 1 timesInput: arr[] = {1, 1, 1, 2, 3, 4, 9, 9, 10} Output: 10 occur
11 min read
Print numbers in descending order along with their frequencies Given an array arr, the task is to print the elements of the array in descending order along with their frequencies.Examples:  Input: arr[] = {1, 3, 3, 3, 4, 4, 5} Output: 5 occurs 1 times 4 occurs 2 times 3 occurs 3 times 1 occurs 1 timesInput: arr[] = {1, 1, 1, 2, 3, 4, 9, 9, 10} Output: 10 occur
11 min read
Replace each element of Array with it's corresponding rank Given an array arr[] of n integers, the task is to replace each element of Array with their rank in array. The rank of an element is defined as the distance between the element with the first element of the array when the array is arranged in ascending order. If two or more are same in the array the
13 min read
Rank of elements in a stream Given a stream of integers, the task is to find the rank of each integer in the stream. Rank of an element x is defined as the number of distinct values less than x.Example: Input : arr[] = [5, 1, 4, 4, 5, 9, 7, 13, 3]Output : 0 0 1 1 2 3 3 5 1Explanation: 5 arrives: rank = 0 (no elements less than
12 min read
Count of array elements whose order of deletion precedes order of insertion Given an initial array, A[] and a final array B[] both of size N containing integers from the range [1, N], where A[] represent the order in which elements were inserted and B[] represents the order in which they were removed, the task is to find the number of elements in B[] lying at an index earli
7 min read