GeeksforGeeks App
Open App
Browser
Continue

# Find relative rank of each element in array

Given an array A[] of N integers, the task is to find the relative rank for each element in the given array.

The relative rank for each element in the array is the count of elements which is greater than the current element in the Longest Increasing Subsequence from the current element.

Examples:

Input: A[] = {8, 16, 5, 6, 9}, N = 5
Output: {1, 0, 2, 1, 0}
Explanation:
For i = 0, required sequence is {8, 16} Relative Rank = 1.
For i = 1, Since all elements after 16 are smaller than 16, Relative Rank = 0.
For i = 2, required sequence is {5, 6, 9} Relative Rank = 2
For i = 3, required sequence is {6, 9} Relative Rank = 1
For i = 4, required sequence is {9} Relative Rank = 0

Input: A[] = {1, 2, 3, 5, 4}
Output: {3, 2, 1, 0, 0}
Explanation:
For i = 0, required sequence is {1, 2, 3, 5}, Relative Rank = 3
For i = 1, required sequence is {2, 3, 5}, Relative Rank = 2
For i = 2, required sequence is {3, 5}, Relative Rank = 1
For i = 3, required sequence is {5}, Relative Rank = 0
For i = 4, required sequence is {4}, Relative Rank = 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The idea is to generate the longest increasing subsequence for each element and then, the relative rank for each element is the (length of LIS – 1).

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use a Stack and store the elements in non-decreasing order from the right to each element(say A[i]) then the rank for each A[i] is the (size of stack – 1) till that element. Below is the illustration of the same:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find relative rank for``// each element in the array A[]``void` `findRank(``int` `A[], ``int` `N)``{``    ``// Create Rank Array``    ``int` `rank[N] = {};` `    ``// Stack to store numbers in``    ``// non-decreasing order from right``    ``stack<``int``> s;` `    ``// Push last element in stack``    ``s.push(A[N - 1]);` `    ``// Iterate from second last``    ``// element to first element``    ``for` `(``int` `i = N - 2; i >= 0; i--) {` `        ``// If current element is less``        ``// than the top of stack and``        ``// push A[i] in stack``        ``if` `(A[i] < s.top()) {` `            ``s.push(A[i]);` `            ``// Rank is stack size - 1``            ``// for current element``            ``rank[i] = s.size() - 1;``        ``}``        ``else` `{` `            ``// Pop elements from stack``            ``// till current element is``            ``// greater than the top``            ``while` `(!s.empty()``                   ``&& A[i] >= s.top()) {``                ``s.pop();``            ``}` `            ``// Push current element in Stack``            ``s.push(A[i]);` `            ``// Rank is stack size - 1``            ``rank[i] = s.size() - 1;``        ``}``    ``}` `    ``// Print rank of all elements``    ``for` `(``int` `i = 0; i < N; i++) {``        ``cout << rank[i] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given array A[]``    ``int` `A[] = { 1, 2, 3, 5, 4 };` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A[0]);` `    ``// Function call``    ``findRank(A, N);``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.io.*;``import` `java.util.*;``import` `java.lang.*;` `class` `GFG{``    ` `// Function to find relative rank for``// each element in the array A[]``static` `void` `findRank(``int``[] A, ``int` `N)``{``    ` `    ``// Create Rank Array``    ``int``[] rank = ``new` `int``[N];` `    ``// Stack to store numbers in``    ``// non-decreasing order from right``    ``Stack s = ``new` `Stack();` `    ``// Push last element in stack``    ``s.add(A[N - ``1``]);` `    ``// Iterate from second last``    ``// element to first element``    ``for``(``int` `i = N - ``2``; i >= ``0``; i--)``    ``{``        ` `        ``// If current element is less``        ``// than the top of stack and``        ``// push A[i] in stack``        ``if` `(A[i] < s.peek())``        ``{``            ``s.add(A[i]);` `            ``// Rank is stack size - 1``            ``// for current element``            ``rank[i] = s.size() - ``1``;``        ``}``        ``else``        ``{` `            ``// Pop elements from stack``            ``// till current element is``            ``// greater than the top``            ``while` `(!s.isEmpty() &&``                    ``A[i] >= s.peek())``            ``{``                ``s.pop();``            ``}` `            ``// Push current element in Stack``            ``s.add(A[i]);` `            ``// Rank is stack size - 1``            ``rank[i] = s.size() - ``1``;``        ``}``    ``}` `    ``// Print rank of all elements``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``System.out.print(rank[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array A[]``    ``int` `A[] = { ``1``, ``2``, ``3``, ``5``, ``4` `};` `    ``int` `N = A.length;` `    ``// Function call``    ``findRank(A, N);``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to find relative rank for``# each element in the array A[]``def` `findRank(A, N):``    ` `    ``# Create Rank Array``    ``rank ``=` `[``0``] ``*` `N` `    ``# Stack to store numbers in``    ``# non-decreasing order from right``    ``s ``=` `[]` `    ``# Push last element in stack``    ``s.append(A[N ``-` `1``])` `    ``# Iterate from second last``    ``# element to first element``    ``for` `i ``in` `range``(N ``-` `2``, ``-``1``, ``-``1``):` `        ``# If current element is less``        ``# than the top of stack and``        ``# append A[i] in stack``        ``if` `(A[i] < s[``-``1``]):``            ``s.append(A[i])` `            ``# Rank is stack size - 1``            ``# for current element``            ``rank[i] ``=` `len``(s) ``-` `1` `        ``else``:` `            ``# Pop elements from stack``            ``# till current element is``            ``# greater than the top``            ``while` `(``len``(s) > ``0` `and` `A[i] >``=` `s[``-``1``]):``                ``del` `s[``-``1``]` `            ``# Push current element in Stack``            ``s.append(A[i])` `            ``# Rank is stack size - 1``            ``rank[i] ``=` `len``(s) ``-` `1` `    ``# Print rank of all elements``    ``for` `i ``in` `range``(N):``        ``print``(rank[i], end ``=` `" "``)``        ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``# Given array A[]``    ``A ``=` `[ ``1``, ``2``, ``3``, ``5``, ``4` `]` `    ``N ``=` `len``(A)` `    ``# Function call``    ``findRank(A, N)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement``// the above approach``using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `class` `GFG{``    ` `// Function to find relative rank for``// each element in the array A[]``static` `void` `findRank(``int``[] A, ``int` `N)``{``    ` `    ``// Create Rank Array``    ``int``[] rank = ``new` `int``[N];` `    ``// Stack to store numbers in``    ``// non-decreasing order from right``    ``Stack<``int``> s = ``new` `Stack<``int``>();` `    ``// Push last element in stack``    ``s.Push(A[N - 1]);` `    ``// Iterate from second last``    ``// element to first element``    ``for``(``int` `i = N - 2; i >= 0; i--)``    ``{` `        ``// If current element is less``        ``// than the top of stack and``        ``// push A[i] in stack``        ``if` `(A[i] < s.Peek())``        ``{``            ``s.Push(A[i]);` `            ``// Rank is stack size - 1``            ``// for current element``            ``rank[i] = s.Count() - 1;``        ``}``        ``else``        ``{` `            ``// Pop elements from stack``            ``// till current element is``            ``// greater than the top``            ``while` `(s.Count() != 0 &&``                   ``A[i] >= s.Peek())``            ``{``                ``s.Pop();``            ``}` `            ``// Push current element in Stack``            ``s.Push(A[i]);` `            ``// Rank is stack size - 1``            ``rank[i] = s.Count() - 1;``        ``}``    ``}` `    ``// Print rank of all elements``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ``Console.Write(rank[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Given array A[]``    ``int``[] A = ``new` `int``[] { 1, 2, 3, 5, 4 };` `    ``int` `N = A.Length;` `    ``// Function call``    ``findRank(A, N);``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 `class GFG``{``    ``// Function to find relative rank for``    ``// each element in the array A[]``    ``static findRank(A, N)``    ``{``        ``// Create Rank Array``        ``var` `rank = Array(N).fill(0);``        ` `        ``// Stack to store numbers in``        ``// non-decreasing order from right``        ``var` `s = Array();``        ` `        ``// Push last element in stack``        ``(s.push(A[N - 1]) > 0);``        ` `        ``// Iterate from second last``        ``// element to first element``        ``for` `(``var` `i = N - 2; i >= 0; i--)``        ``{``        ` `            ``// If current element is less``            ``// than the top of stack and``            ``// push A[i] in stack``            ``if` `(A[i] < s[s.length-1])``            ``{``                ``(s.push(A[i]) > 0);``                ` `                ``// Rank is stack size - 1``                ``// for current element``                ``rank[i] = s.length - 1;``            ``}``            ``else``            ``{``                ``// Pop elements from stack``                ``// till current element is``                ``// greater than the top``                ``while` `(!(s.length == 0) && A[i] >= s[s.length-1])``                ``{``                    ``s.pop();``                ``}``                ` `                ``// Push current element in Stack``                ``(s.push(A[i]) > 0);``                ` `                ``// Rank is stack size - 1``                ``rank[i] = s.length - 1;``            ``}``        ``}``        ` `        ``// Print rank of all elements``        ``for` `(``var` `i=0; i < N; i++)``        ``{``            ``console.log(rank[i] + ``" "``);``        ``}``    ``}``    ` `    ``// Driver Code``    ``static main(args)``    ``{``    ` `        ``// Given array A[]``        ``var` `A = [1, 2, 3, 5, 4];``        ``var` `N = A.length;``        ` `        ``// Function call``        ``GFG.findRank(A, N);``    ``}``}``GFG.main([]);` `// This code is contributed by aadityaburujwale.`

Output:

`3 2 1 0 0`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up