# Count of larger elements on right side of each element in an array

• Difficulty Level : Hard
• Last Updated : 01 Jun, 2022

Given an array arr[] consisting of N integers, the task is to count the number of greater elements on the right side of each array element.

Examples:

Input: arr[] = {3, 7, 1, 5, 9, 2} Output: {3, 1, 3, 1, 0, 0} Explanation: For arr, the elements greater than it on the right are {7, 5, 9}. For arr, the only element greater than it on the right is {9}. For arr, the elements greater than it on the right are {5, 9, 2}. For arr, the only element greater than it on the right is {9}. For arr and arr, no greater elements exist on the right.

Input: arr[] = {5, 4, 3, 2} Output: {0, 0, 0, 0}

Naive Approach: The simplest approach is to iterate all array elements using two loops and for each array element, count the number of elements greater than it on its right side and then print it. Time Complexity: O(N2) Auxiliary Space: O(1)

Efficient Approach: The problem can be solved using the concept of Merge Sort in descending order. Follow the steps given below to solve the problem:

• Initialize an array count[] where count[i] store the respective count of greater elements on the right for every arr[i]
• Take the indexes i and j, and compare the elements in an array.
• If higher index element is greater than the lower index element then, all the higher index element will be greater than all the elements after that lower index.
• Since the left part is already sorted, add the count of elements after the lower index element to the count[] array for the lower index.
• Repeat the above steps until the entire array is sorted.
• Finally print the values of count[] array.

Below is the implementation of the above approach:

## Java

 `// Java program for the above approach`   `import` `java.util.*;`   `public` `class` `GFG {`   `    ``// Stores the index & value pairs` `    ``static` `class` `Item {`   `        ``int` `val;` `        ``int` `index;`   `        ``public` `Item(``int` `val, ``int` `index)` `        ``{` `            ``this``.val = val;` `            ``this``.index = index;` `        ``}` `    ``}`   `    ``// Function to count the number of` `    ``// greater elements on the right` `    ``// of every array element` `    ``public` `static` `ArrayList` `    ``countLarge(``int``[] a)` `    ``{` `        ``// Length of the array` `        ``int` `len = a.length;`   `        ``// Stores the index-value pairs` `        ``Item[] items = ``new` `Item[len];`   `        ``for` `(``int` `i = ``0``; i < len; i++) {` `            ``items[i] = ``new` `Item(a[i], i);` `        ``}`   `        ``// Stores the count of greater` `        ``// elements on right` `        ``int``[] count = ``new` `int``[len];`   `        ``// Perform MergeSort operation` `        ``mergeSort(items, ``0``, len - ``1``,` `                  ``count);`   `        ``ArrayList res` `            ``= ``new` `ArrayList<>();`   `        ``for` `(``int` `i : count) {` `            ``res.add(i);` `        ``}`   `        ``return` `res;` `    ``}`   `    ``// Function to sort the array` `    ``// using Merge Sort` `    ``public` `static` `void` `mergeSort(` `        ``Item[] items, ``int` `low ``int` `high,` `        ``int``[] count)` `    ``{`   `        ``// Base Case` `        ``if` `(low >= high) {` `            ``return``;` `        ``}`   `        ``// Find Mid` `        ``int` `mid = low + (high - low) / ``2``;`   `        ``mergeSort(items, low, mid,` `                  ``count);` `        ``mergeSort(items, mid + ``1``,` `                  ``high, count);`   `        ``// Merging step` `        ``merge(items, low, mid,` `              ``mid + ``1``, high, count);` `    ``}`   `    ``// Utility function to merge sorted` `    ``// subarrays and find the count of` `    ``// greater elements on the right` `    ``public` `static` `void` `merge(` `        ``Item[] items, ``int` `low, ``int` `lowEnd,` `        ``int` `high, ``int` `highEnd, ``int``[] count)` `    ``{` `        ``int` `m = highEnd - low + ``1``; ``// mid`   `        ``Item[] sorted = ``new` `Item[m];`   `        ``int` `rightCounter = ``0``;` `        ``int` `lowInd = low, highInd = high;` `        ``int` `index = ``0``;`   `        ``// Loop to store the count of` `        ``// larger elements on right side` `        ``// when both array have elements` `        ``while` `(lowInd <= lowEnd` `               ``&& highInd <= highEnd) {`   `            ``if` `(items[lowInd].val` `                ``< items[highInd].val) {` `                ``rightCounter++;` `                ``sorted[index++]` `                    ``= items[highInd++];` `            ``}` `            ``else` `{` `                ``count[items[lowInd].index] += rightCounter;` `                ``sorted[index++] = items[lowInd++];` `            ``}` `        ``}`   `        ``// Loop to store the count of` `        ``// larger elements in right side` `        ``// when only left array have` `        ``// some element` `        ``while` `(lowInd <= lowEnd) {`   `            ``count[items[lowInd].index] += rightCounter;` `            ``sorted[index++] = items[lowInd++];` `        ``}`   `        ``// Loop to store the count of` `        ``// larger elements in right side` `        ``// when only right array have` `        ``// some element` `        ``while` `(highInd <= highEnd) {`   `            ``sorted[index++] = items[highInd++];` `        ``}`   `        ``System.arraycopy(sorted, ``0``, items,` `                         ``low, m);` `    ``}`   `    ``// Utility function that prints` `    ``// the count of greater elements` `    ``// on the right` `    ``public` `static` `void` `    ``printArray(ArrayList countList)` `    ``{`   `        ``for` `(Integer i : countList)` `            ``System.out.print(i + ``" "``);`   `        ``System.out.println();` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Given array` `        ``int` `arr[] = { ``3``, ``7``, ``1``, ``5``, ``9``, ``2` `};` `        ``int` `n = arr.length;`   `        ``// Function Call` `        ``ArrayList countList` `            ``= countLarge(arr);`   `        ``printArray(countList);` `    ``}` `}`

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

Another approach: We can use binary search to solve this. The idea is to create a sorted list of input and then for each element of input we first remove that element from the sorted list and then apply the modified binary search to find the element just greater than the current element and then the number of large elements will be the difference between the found index & the length of sorted list.

## C#

 `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG{` `    ``static` `public` `void` `Main (){` `        ``//Code` `          ``var` `arr = ``new` `List<``int``>(){3, 7, 1, 5, 9, 2};` `        ``var` `res = CountLarge(arr);` `        ``PrintArray(res);` `    ``}` `    ``public` `static` `List<``int``> CountLarge(List<``int``> list)` `    ``{` `        ``var` `sortedList = ``new` `List<``int``>(list);` `        ``sortedList.Sort();` `        ``for``(``int` `i=0;i list){` `        ``int` `l=0,r=list.Count-1,mid;` `        ``while``(l item)` `                ``r = mid;` `            ``else` `                ``l = mid + 1;` `        ``}` `        ``if``(l==r && item > list[l])` `            ``return` `0;` `        ``return` `list.Count-l;` `    ``}` `    ``public` `static` `void` `DeleteItemFromSortedList(List<``int``> list, ``int` `item){` `        ``var` `index = BinarySearch(list, item);` `        ``list.RemoveAt(index);` `    ``}` `    ``public` `static` `int` `BinarySearch(List<``int``> list, ``int` `item){` `        ``int` `l=0,r=list.Count-1,mid;` `        ``while``(l<=r){` `            ``mid = l + (r-l)/2;` `            ``if``(list[mid] == item)` `                ``return` `mid;` `            ``else` `if``(list[mid] < item)` `                ``l = mid + 1;` `            ``else` `                ``r = mid - 1;` `        ``}` `        ``return` `-1;` `    ``}` `    ``public` `static` `void` `PrintArray(List<``int``> list)` `    ``{` `        ``foreach``(``var` `item ``in` `list)` `            ``Console.Write(item + ``" "``);` `    ``}` `}`

Output

`5 2 6 3 1 3 0 1 0 `

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :