# Count Smaller Elements on The Right Side in Java

An array or list inArr of the numbers (either negative or positive) is given. The task is to find the number of smaller elements that exist on the right side of the current element. The following examples are given for a better understanding.

Example 1:

Input

int inArr[] = {7, 4, 9, 1, 3, 5, 0, 6, 2, 8}

Output: outArr[] = {7, 4, 7, 1, 2, 2, 0, 1, 0, 0}

Explanation: For element 7, the number of smaller elements on the right side is 7 (4, 1, 3, 5, 0, 6, 2). For element 4, the number of smaller elements on the right side is 4 (1, 3, 0, 2). All the element on the right side of the number 9 is smaller than 9, and its count is 7. For element 1, only 0 is smaller on the right side. Hence, the count is 1. For 3, only two elements (0, 2) are smaller than 3. For element 5, only 0 and 2 are smaller than 5 and exist on the right side. For 0, no smaller element exists. Thus, making the count 0. For 6, only 2 is smaller than 6 (count is 1). For element 2, only 8 exits on the right side which is greater than 2. Therefore, the count is 0. For element 8, there is no element on the right side, making the count 0.

Example 2:

Input

int inArr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}

Output: outArr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}

Explanation: All the elements are arranged in strictly decreasing order. Also, the position of the elements in the input array is such that the count of elements on the right side is equal to the value of the element itself. For example, the number of elements on the right side of element 1 is 1 (only 0 exists on the right side). Hence, the input array becomes the answer itself.

Example 3:

Input

int inArr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Output: outArr[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Explanation: The elements are arranged in increasing order. Thus, no small elements exist on the right side if we take an element of the input array as a reference. Hence, the output array only contains 0.

Example 4:

Input

int inArr[] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}

Output: outArr[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Explanation: All the elements of the input array are constant, i.e., of the same value. Thus, no small element exists in the input array. Therefore, all the elements of the output array are zero.

## Approach: Using Nested Loop

In this approach, we will be nesting two for-loops. The outer loop picks the element from the input array (consider it as the current element). The inner loop starts from the element next to the current element and goes all the way to the end. In the inner loop, compare the elements pointed by the inner loop variable with the current element. If it is small, increase the count for the current element by 1; otherwise, do not increase the count.

FileName: CountSmallEle.java

Output:

```For the input array:
7 4 9 1 3 5 0 6 2 8
The count of the smaller element on the right side is:
7 4 7 1 2 2 0 1 0 0

For the input array:
9 8 7 6 5 4 3 2 1 0
The count of the smaller element on the right side is:
9 8 7 6 5 4 3 2 1 0

For the input array:
0 1 2 3 4 5 6 7 8 9
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0

For the input array:
4 4 4 4 4 4 4 4 4 4
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0
```

Complexity Analysis: Since the program uses nested loops, the time complexity of the program is O(N2). The program is also using extra space for storing the output. Thus, the space complexity of the program is also O(N), where N is the total number of elements present in the input array.

## Approach: Using BST With Some Extra Fields

Another approach is to the simple Binary Search Tree (BST) with some of the extra fields. Those two extra fields are 1) For holding the elements on the left side of a node and 2) For keeping the element occurrence count. We will be traversing the given array from the end to the beginning and adding the elements into the Binary Search Tree. While putting the elements in the Binary Search Tree, we will be computing the count of the elements that are less than that of the current element.

### Implementation

Observe the following program.

FileName: CountSmallEle1.java

Output:

```For the input array:
7 4 9 1 3 5 0 6 2 8
The count of the smaller element on the right side is:
7 4 7 1 2 2 0 1 0 0

For the input array:
9 8 7 6 5 4 3 2 1 0
The count of the smaller element on the right side is:
9 8 7 6 5 4 3 2 1 0

For the input array:
0 1 2 3 4 5 6 7 8 9
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0

For the input array:
4 4 4 4 4 4 4 4 4 4
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0
```

Complexity Analysis: Since the added step can take O(n) time, the time complexity of the program is O(n2). The program is also using an auxiliary array for storing the result, making the space complexity of the program O(n), where n is the total number of elements present in the array.

We can do the optimization to reduce the time complexity. The following approach shows the same.

## Approach: Using Self Balancing Tree

The drawback of the BST is that it is not a self-balancing tree. Thus, BST can become a skew tree leading to higher time complexity. Therefore, to do further optimization, it is required to use a self-balancing tree to compute the answer.

We will insert all the elements in the self-balancing tree one by one by traversing the input array from right to left. Whenever a new key is inserted, we check or compare it with the root element (the root element is the right-most element of the input array). If the new key is larger than the root, then it means it is larger than all of the nodes that are residing on the left side of the root node. Therefore, the size of the left subtree gets added to the count of the smaller elements for the new key that is getting inserted. The same approach is getting followed for the other nodes too.

### Implementation

Observe the following program.

FileName: CountSmallEle1.java

Output:

```For the input array:
7 4 9 1 3 5 0 6 2 8
The count of the smaller element on the right side is:
7 4 7 1 2 2 0 1 0 0

For the input array:
9 8 7 6 5 4 3 2 1 0
The count of the smaller element on the right side is:
9 8 7 6 5 4 3 2 1 0

For the input array:
0 1 2 3 4 5 6 7 8 9
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0

For the input array:
4 4 4 4 4 4 4 4 4 4
The count of the smaller element on the right side is:
0 0 0 0 0 0 0 0 0 0
```

Complexity Analysis: The binary tree used in the above program can never be the skew tree. Thus, the time complexity of the program is O(n x log(n)), where n is the total number of nodes present in the input array. The space complexity of the program is the same as the previous program.

For Videos Join Our Youtube Channel: Join Now