Count of larger elements on right side of each element in an array
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[0], the elements greater than it on the right are {7, 5, 9}.
For arr[1], the only element greater than it on the right is {9}.
For arr[2], the elements greater than it on the right are {5, 9, 2}.
For arr[3], the only element greater than it on the right is {9}.
For arr[4] and arr[5], 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<Integer> countLarge(int[] a) { // Lemgth 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<Integer> 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<Integer> 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<Integer> countList = countLarge(arr); printArray(countList); }} |
3 1 3 1 0 0
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


