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) { // 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<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)
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.Count;i++){ DeleteItemFromSortedList(sortedList, list[i]); list[i] = CountLargeNumbers(list[i], sortedList); } return list; } public static int CountLargeNumbers( int item, List< int > list){ int l=0,r=list.Count-1,mid; while (l<r){ mid = l + (r-l)/2; if (list[mid] > 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 + " " ); } } |
Javascript
<script> function main() { //Code const arr = [3, 7, 1, 5, 9, 2]; const res = countLarge(arr); printArray(res); } function countLarge(list) { const sortedList = [...list]; sortedList.sort(); for (let i = 0; i < list.length; i++) { deleteItemFromSortedList(sortedList, list[i]); list[i] = countLargeNumbers(list[i], sortedList); } return list; } function countLargeNumbers(item, list) { let l = 0; let r = list.length - 1; let mid; while (l < r) { mid = l + Math.floor((r - l) / 2); if (list[mid] > item) r = mid; else l = mid + 1; } if (l === r && item > list[l]) return 0; return list.length - l; } function deleteItemFromSortedList(list, item) { const index = binarySearch(list, item); list.splice(index, 1); } function binarySearch(list, item) { let l = 0; let r = list.length - 1; let mid; while (l <= r) { mid = l + Math.floor((r - l) / 2); if (list[mid] === item) return mid; else if (list[mid] < item) l = mid + 1; else r = mid - 1; } return -1; } function printArray(list) { for (const item of list) { document.write(item + " " ); } } main(); </script> |
3 1 3 1 0 0
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Please Login to comment...