# Rank of an element in a stream

Given a stream of integers, lookup the rank of a given integer x. Rank of an integer in stream is “Total number of elements less than or equal to x (not including x)”.

If element is not found in stream or is smallest in stream, return -1.
Examples:

```Input :  arr[] = {10, 20, 15, 3, 4, 4, 1}
x = 4;
Output : Rank of 4 in stream is: 3
There are total three elements less than
or equal to x (and not including x)

Input : arr[] = {5, 1, 14, 4, 15, 9, 7, 20, 11},
x = 20;
Output : Rank of 20 in stream is: 8
```

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

A relatively easy way to implement this is to use an Array that holds all the elements in sorted order. When a new element is inserted we would shift the elements. Then we perform binary search on the array to get right most index of x and return that index. getRank(x) would work in O(log n) but insertion would be costly.

An efficient way is to use a Binary Search Tree. Each Node will hold the data value and size of its left subtree.

We traverse the tree from root and compare the root values to x.

1. If root->data == x, return size of left subtree of root.
2. If x < root->data, return getRank(root->left)
3. If x > root->data, return getRank(root->right) + size of letSubtree + 1.

Below is the solution.

## C++

 `// CPP program to find rank of an ` `// element in a stream. ` `#include ` `using` `namespace` `std; ` ` `  `struct` `Node { ` `    ``int` `data; ` `    ``Node *left, *right; ` `    ``int` `leftSize; ` `}; ` ` `  `Node* newNode(``int` `data) ` `{ ` `    ``Node *temp = ``new` `Node; ` `    ``temp->data = data; ` `    ``temp->left = temp->right = NULL; ` `    ``temp->leftSize = 0; ` `    ``return` `temp; ` `} ` ` `  `// Inserting a new Node. ` `Node* insert(Node*& root, ``int` `data) ` `{ ` `    ``if` `(!root) ` `        ``return` `newNode(data); ` ` `  `    ``// Updating size of left subtree. ` `    ``if` `(data <= root->data) { ` `        ``root->left = insert(root->left, data); ` `        ``root->leftSize++; ` `    ``} ` `    ``else` `        ``root->right = insert(root->right, data); ` ` `  `    ``return` `root; ` `} ` ` `  `// Function to get Rank of a Node x. ` `int` `getRank(Node* root, ``int` `x) ` `{ ` `    ``// Step 1. ` `    ``if` `(root->data == x) ` `        ``return` `root->leftSize; ` ` `  `    ``// Step 2. ` `    ``if` `(x < root->data) { ` `        ``if` `(!root->left) ` `            ``return` `-1; ` `        ``else` `            ``return` `getRank(root->left, x); ` `    ``} ` ` `  `    ``// Step 3. ` `    ``else` `{ ` `        ``if` `(!root->right) ` `            ``return` `-1; ` `        ``else` `{ ` `            ``int` `rightSize = getRank(root->right, x); ` `            ``return` `root->leftSize + 1 + rightSize; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 1, 4, 4, 5, 9, 7, 13, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `x = 4; ` ` `  `    ``Node* root = NULL; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``root = insert(root, arr[i]); ` ` `  `    ``cout << ``"Rank of "` `<< x << ``" in stream is: "`  `         ``<< getRank(root, x) << endl; ` ` `  `    ``x = 13; ` `    ``cout << ``"Rank of "` `<< x << ``" in stream is: "` `         ``<< getRank(root, x) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find rank of an  ` `// element in a stream.  ` `class` `GfG {  ` ` `  `static` `class` `Node {  ` `    ``int` `data;  ` `    ``Node left, right;  ` `    ``int` `leftSize;  ` `}  ` ` `  `static` `Node newNode(``int` `data)  ` `{  ` `    ``Node temp = ``new` `Node();  ` `    ``temp.data = data;  ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``;  ` `    ``temp.leftSize = ``0``;  ` `    ``return` `temp;  ` `}  ` ` `  `// Inserting a new Node.  ` `static` `Node insert(Node root, ``int` `data)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return` `newNode(data);  ` ` `  `    ``// Updating size of left subtree.  ` `    ``if` `(data <= root.data) {  ` `        ``root.left = insert(root.left, data);  ` `        ``root.leftSize++;  ` `    ``}  ` `    ``else` `        ``root.right = insert(root.right, data);  ` ` `  `    ``return` `root;  ` `}  ` ` `  `// Function to get Rank of a Node x.  ` `static` `int` `getRank(Node root, ``int` `x)  ` `{  ` `    ``// Step 1.  ` `    ``if` `(root.data == x)  ` `        ``return` `root.leftSize;  ` ` `  `    ``// Step 2.  ` `    ``if` `(x < root.data) {  ` `        ``if` `(root.left == ``null``)  ` `            ``return` `-``1``;  ` `        ``else` `            ``return` `getRank(root.left, x);  ` `    ``}  ` ` `  `    ``// Step 3.  ` `    ``else` `{  ` `        ``if` `(root.right == ``null``)  ` `            ``return` `-``1``;  ` `        ``else` `{  ` `            ``int` `rightSize = getRank(root.right, x);  ` `            ``return` `root.leftSize + ``1` `+ rightSize;  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `arr[] = { ``5``, ``1``, ``4``, ``4``, ``5``, ``9``, ``7``, ``13``, ``3` `};  ` `    ``int` `n = arr.length;  ` `    ``int` `x = ``4``;  ` ` `  `    ``Node root = ``null``;  ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``root = insert(root, arr[i]);  ` ` `  `    ``System.out.println(``"Rank of "` `+ x + ``" in stream is : "``+getRank(root, x));  ` ` `  `    ``x = ``13``;  ` `    ``System.out.println(``"Rank of "` `+ x + ``" in stream is : "``+getRank(root, x));  ` ` `  `}  ` `}  `

## Python3

 `# Python3 program to find rank of an  ` `# element in a stream.  ` ` `  `class` `newNode: ` `    ``def` `__init__(``self``, data): ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `self``.right ``=` `None` `        ``self``.leftSize ``=` `0` ` `  `# Inserting a new Node.  ` `def` `insert(root, data): ` `    ``if` `root ``=``=` `None``:  ` `        ``return` `newNode(data)  ` ` `  `    ``# Updating size of left subtree.  ` `    ``if` `data <``=` `root.data:  ` `        ``root.left ``=` `insert(root.left, data)  ` `        ``root.leftSize ``+``=` `1` `    ``else``: ` `        ``root.right ``=` `insert(root.right, data) ` `    ``return` `root ` ` `  `# Function to get Rank of a Node x.  ` `def` `getRank(root, x): ` `     `  `    ``# Step 1.  ` `    ``if` `root.data ``=``=` `x: ` `        ``return` `root.leftSize  ` ` `  `    ``# Step 2.  ` `    ``if` `x < root.data:  ` `        ``if` `root.left ``=``=` `None``:  ` `            ``return` `-``1` `        ``else``: ` `            ``return` `getRank(root.left, x) ` ` `  `    ``# Step 3.  ` `    ``else``:  ` `        ``if` `root.right ``=``=` `None``:  ` `            ``return` `-``1` `        ``else``:  ` `            ``rightSize ``=` `getRank(root.right, x)  ` `            ``return` `root.leftSize ``+` `1` `+` `rightSize ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``5``, ``1``, ``4``, ``4``, ``5``, ``9``, ``7``, ``13``, ``3``]  ` `    ``n ``=` `len``(arr)  ` `    ``x ``=` `4` ` `  `    ``root ``=` `None` `    ``for` `i ``in` `range``(n): ` `        ``root ``=` `insert(root, arr[i]) ` ` `  `    ``print``(``"Rank of"``, x, ``"in stream is:"``,  ` `                       ``getRank(root, x)) ` `    ``x ``=` `13` `    ``print``(``"Rank of"``, x, ``"in stream is:"``,  ` `                       ``getRank(root, x)) ` ` `  `# This code is contributed by PranchalK `

## C#

 `// C# program to find rank of an  ` `// element in a stream.  ` `using` `System; ` `     `  `class` `GFG  ` `{  ` `public` `class` `Node  ` `{  ` `    ``public` `int` `data;  ` `    ``public` `Node left, right;  ` `    ``public` `int` `leftSize;  ` `}  ` ` `  `static` `Node newNode(``int` `data)  ` `{  ` `    ``Node temp = ``new` `Node();  ` `    ``temp.data = data;  ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``;  ` `    ``temp.leftSize = 0;  ` `    ``return` `temp;  ` `}  ` ` `  `// Inserting a new Node.  ` `static` `Node insert(Node root, ``int` `data)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return` `newNode(data);  ` ` `  `    ``// Updating size of left subtree.  ` `    ``if` `(data <= root.data)  ` `    ``{  ` `        ``root.left = insert(root.left, data);  ` `        ``root.leftSize++;  ` `    ``}  ` `    ``else` `        ``root.right = insert(root.right, data);  ` ` `  `    ``return` `root;  ` `}  ` ` `  `// Function to get Rank of a Node x.  ` `static` `int` `getRank(Node root, ``int` `x)  ` `{  ` `    ``// Step 1.  ` `    ``if` `(root.data == x)  ` `        ``return` `root.leftSize;  ` ` `  `    ``// Step 2.  ` `    ``if` `(x < root.data)  ` `    ``{  ` `        ``if` `(root.left == ``null``)  ` `            ``return` `-1;  ` `        ``else` `            ``return` `getRank(root.left, x);  ` `    ``}  ` ` `  `    ``// Step 3.  ` `    ``else`  `    ``{  ` `        ``if` `(root.right == ``null``)  ` `            ``return` `-1;  ` `        ``else`  `        ``{  ` `            ``int` `rightSize = getRank(root.right, x);  ` `            ``return` `root.leftSize + 1 + rightSize;  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `[]arr = { 5, 1, 4, 4, 5, 9, 7, 13, 3 };  ` `    ``int` `n = arr.Length;  ` `    ``int` `x = 4;  ` ` `  `    ``Node root = ``null``;  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``root = insert(root, arr[i]);  ` ` `  `    ``Console.WriteLine(``"Rank of "` `+ x +  ` `                      ``" in stream is : "` `+  ` `                      ``getRank(root, x));  ` ` `  `    ``x = 13;  ` `    ``Console.WriteLine(``"Rank of "` `+ x +  ` `                      ``" in stream is : "` `+  ` `                      ``getRank(root, x));  ` `}  ` `}  ` ` `  `// This code is contributed by PrinciRaj1992  `

Output:

```Rank of 4 in stream is: 3
Rank of 13 in stream is: 8
```

Another approach:
Traverse the array from the beginning. While traversing, count the nodes which is equal to or less than the given key.Print the count(Rank).

## C++

 `// C++ program to find rank of an  ` `// element in a stream.  ` `#include ` `using` `namespace` `std; ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = {5, 1, 14, 4, 15, 9, 7, 20, 11}; ` `    ``int` `key = 20; ` `    ``int` `arraySize = ``sizeof``(a)/``sizeof``(a); ` `    ``int` `count = 0; ` `    ``for``(``int` `i = 0; i < arraySize; i++) ` `    ``{ ` `        ``if``(a[i] <= key) ` `        ``{ ` `            ``count += 1; ` `        ``} ` `    ``} ` `    ``cout << ``"Rank of "` `<< key << ``" in stream is: "`  `            ``<< count-1 << endl; ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by  ` `// Ashwin Loganathan. `

## Java

 `// Java program to find rank of an  ` `// element in a stream. ` `class` `GFG  ` `{ ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `a[] = {``5``, ``1``, ``14``, ``4``, ``15``, ``9``, ``7``, ``20``, ``11``};  ` `    ``int` `key = ``20``;  ` `    ``int` `arraySize = a.length;  ` `    ``int` `count = ``0``;  ` `    ``for``(``int` `i = ``0``; i < arraySize; i++)  ` `    ``{  ` `        ``if``(a[i] <= key)  ` `        ``{  ` `            ``count += ``1``;  ` `        ``}  ` `    ``}  ` `    ``System.out.println(``"Rank of "` `+ key +  ` `                    ``" in stream is: "` `+ (count - ``1``)); ` `} ` `}  ` ` `  `// This code has been contributed by 29AjayKumar `

## Python3

 `# Python3 program to find rank of an  ` `# element in a stream.  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `[``5``, ``1``, ``14``, ``4``, ``15``, ` `            ``9``, ``7``, ``20``, ``11``]  ` `    ``key ``=` `20` `    ``arraySize ``=` `len``(a)  ` `    ``count ``=` `0` `    ``for` `i ``in` `range``(arraySize): ` `        ``if` `a[i] <``=` `key: ` `            ``count ``+``=` `1` `             `  `    ``print``(``"Rank of"``, key,  ` `          ``"in stream is:"``, count ``-` `1``) ` ` `  `# This code is contributed by PranchalK `

## C#

 `// C# program to find rank of an  ` `// element in a stream.  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]a = {5, 1, 14, 4, 15, 9, 7, 20, 11}; ` `    ``int` `key = 20; ` `    ``int` `arraySize = a.Length; ` `    ``int` `count = 0; ` `    ``for``(``int` `i = 0; i < arraySize; i++) ` `    ``{ ` `        ``if``(a[i] <= key) ` `        ``{ ` `            ``count += 1; ` `        ``} ` `    ``} ` `    ``Console.WriteLine(``"Rank of "` `+ key +  ` `                      ``" in stream is: "` `+  ` `                            ``(count - 1)); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Akanksha Rai `

## PHP

 ` `

Output:

```Rank of 20 in stream is: 8
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.