Javatpoint Logo
Javatpoint Logo

Count Smaller Elements on Right Side in C++

In this article, we will discuss how to count smaller elements on the right side in C++ with several examples.

Below is the N-dimensional unsorted array arr[], which is made up of unique integers. Our objective is to create a second array count where the count will be smaller. The count of elements on the right side of the array that is smaller than the arr[i] will be shown by the smaller[i] variable.

Sample examples:

Example:1

Input:

N = 5, arr[] = {7, 4, 6, 2, 1}

Output:

{3, 2, 2, 1, 0}

Explanation:

  • For arr[0], there are three elements on the right side that are smaller than 7, which are {4, 6, 2}.
  • For arr[1], there are two elements on the right side that are smaller than 4, which are {2, 1}.
  • For arr[2], there are two elements on the right side that are smaller than 6, which are {2, 1}.
  • For arr[3], there is one element on the right side that is smaller than 2, which is {1}.
  • For arr[4], there are no elements on the right side that are smaller.

Example 2:

Input:

N=5, arr[] = {7, 1, 5, 2, 10}

Output:

{3, 0, 0, 2, 0}

Explanation:

  • Three of the items on the right side are smaller for arr[0].
  • 0 items on the right side are smaller for arr[1].
  • 0 items on the right side are smaller for arr[2].
  • Two of the elements on the right side of arr[3] are smaller.
  • 0 items on the right side of arr[4] are smaller.

Method 1: Brute Force

  • In this method, we are making an array called countSmaller[] and storing the smaller elements on each element's right side will be the brute force method for solving this problem.
  • For each element, the number of smaller items on the right side will be counted using nested for loops.
  • Every time an element appears on the right side of the current element, we will increase the count. When the loop is complete, we will assign countSmaller[i], the value of this count.
  • The countSmaller array will be printed following the completion of both loops.

C++ implementation:

Output:

4 2 3 0 0 0

Method 2: Using Self-Balancing BST

  • We will use self-balancing bst to solve this problem in O(N(log(n))
  • We will traverse the array from left to right to add these components to the tree.
  • We will initially compare the new key with the root when inserting it. The nodes of the root's left subtree would all be greater than the key if it were greater than the root.
  • Therefore, we will increase the number of smaller items for the key being inserted by the size of the left subtree.
  • We will recursively follow this process for all the nodes of the tree.

C++ implementation:

Output:

Input: arr[] = {5, 3, 6, 1, 9}  
Output: 2 1 1 0 0

Method 3: BST with two additional fields

  • Binary Search Tree will contain two additional fields, including: the elements on the left side of the node will be stored in one field, and their frequencies will be kept in the second field.
  • These elements will be added to the tree by right-to-left traversing the array.
  • The number of items that are less than the current ones will be calculated when the new key is inserted. The number of components to the left of the current node and their frequencies added together will be our only calculation in doing this.
  • Once an element is in the proper position, we may calculate its sum.
  • This procedure will be repeated for each node in the tree.

Program:

Output:

4 2 4 1 1 0

Conclusion:

In this article, the issue of counting smaller items on the right side was covered. We have examined three methods for solving this problem: brute force, self-balancing bst, and the last method, which is also optimal but employs bst with two more fields







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA