A surpasser of an element of an array is a greater element to its right, therefore x[j] is a surpasser of x[i] if i < j and x[i] < x[j]. The surpasser count of an element is the number of surpassers. Given an array of distinct integers, for each element of the array find its surpasser count i.e. count the number of elements to the right that are greater than that element.
Examples :
Input: [2, 7, 5, 3, 0, 8, 1]
Output: [4, 1, 1, 1, 2, 0, 0]
Method 1 (Naive): The naive solution would be to run two loops. For each element of the array, we count all elements greater than it to its right. The complexity of this solution is O(n2)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findSurpasser(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = i + 1; j < n; j++)
if (arr[j] > arr[i])
count++;
cout << count << " ";
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 2, 7, 5, 3, 0, 8, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, n);
printf("Surpasser Count of array is \n");
findSurpasser(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void findSurpasser(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = i + 1; j < n; j++)
if (arr[j] > arr[i])
count++;
System.out.print(count +" ");
}
}
static void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print( arr[i] + " ");
System.out.println();
}
public static void main (String[] args)
{
int arr[] = { 2, 7, 5, 3, 0, 8, 1 };
int n = arr.length;
System.out.println("Given array is ");
printArray(arr, n);
System.out.println("Surpasser Count of"
+ " array is ");
findSurpasser(arr, n);
}
}
|
Python3
def findSurpasser(arr, n):
for i in range(0, n):
count = 0;
for j in range (i + 1, n):
if (arr[j] > arr[i]):
count += 1
print(count, end = " ")
def printArray(arr, n):
for i in range(0, n):
print(arr[i], end = " ")
arr = [2, 7, 5, 3, 0, 8, 1 ]
n = len(arr)
print("Given array is")
printArray(arr , n)
print("\nSurpasser Count of array is");
findSurpasser(arr , n)
|
C#
using System;
class GFG {
static void findSurpasser(int []arr, int n)
{
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = i + 1; j < n; j++)
if (arr[j] > arr[i])
count++;
Console.Write(count + " ");
}
}
static void printArray(int []arr, int n)
{
for (int i = 0; i < n; i++)
Console.Write( arr[i] + " ");
Console.WriteLine();
}
public static void Main ()
{
int []arr = { 2, 7, 5, 3, 0, 8, 1 };
int n = arr.Length;
Console.WriteLine("Given array is ");
printArray(arr, n);
Console.WriteLine("Surpasser Count of"
+ " array is ");
findSurpasser(arr, n);
}
}
|
PHP
<?php
function findSurpasser($arr, $n)
{
for ( $i = 0; $i < $n; $i++)
{
$count = 0;
for ( $j = $i + 1; $j < $n; $j++)
if ($arr[$j] > $arr[$i])
$count++;
echo $count , " ";
}
}
function printArray( $arr, $n)
{
for ( $i = 0; $i < $n; $i++)
echo $arr[$i]," ";
echo "\n";
}
$arr = array( 2, 7, 5, 3, 0, 8, 1 );
$n = count($arr);
echo "Given array is \n";
printArray($arr, $n);
echo "Surpasser Count of array is \n";
findSurpasser($arr, $n);
?>
|
Javascript
<script>
function findSurpasser(arr, n)
{
for (let i = 0; i < n; i++)
{
let count = 0;
for (let j = i + 1; j < n; j++)
if (arr[j] > arr[i])
count++;
document.write(count +" ");
}
}
function printArray(arr, n)
{
for (let i = 0; i < n; i++)
document.write( arr[i] + " ");
document.write();
}
let arr = [ 2, 7, 5, 3, 0, 8, 1 ];
let n = arr.length;
document.write("Given array is " + "<br />");
printArray(arr, n);
document.write("<br />");
document.write("Surpasser Count of"
+ " array is " + "<br />");
findSurpasser(arr, n);
</script>
|
Output
Given array is
2 7 5 3 0 8 1
Surpasser Count of array is
4 1 1 1 2 0 0
Time Complexity : O(n2)
Auxiliary Space: O(1) because using constant space for variables
Method 2 (Uses Merge Sort): For any element of the array, we can easily find out number of elements to the right that are greater than that element if we know number of elements to its right that are less than that element. The idea is to count the number of inversions for each element of the array using merge sort. So, surpasser count of an element at position i will be equal to “n – i – inversion-count” at that position where n is the size of the array.
We have already discussed how to find inversion count of complete array here. We have modified the discussed approach to find number of inversions for each element of the array instead of returning inversion count of whole array. Also, as all elements of the array are distinct, we maintain a map that stores inversion count for each element of the array.
Below is C++ implementation of above idea
C++
#include <bits/stdc++.h>
using namespace std;
int merge(int arr[], int l, int m, int r,
unordered_map<int, int> &hm;)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0, j = 0, k = l;
int c = 0;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
hm[L[i]] += c;
arr[k++] = L[i++];
}
else
{
arr[k++] = R[j++];
c++;
}
}
while (i < n1)
{
hm[L[i]] += c;
arr[k++] = L[i++];
}
while (j < n2)
arr[k++] = R[j++];
}
int mergeSort(int arr[], int l, int r,
unordered_map<int, int> &hm;)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m, hm);
mergeSort(arr, m + 1, r, hm);
merge(arr, l, m, r, hm);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
void findSurpasser(int arr[], int n)
{
unordered_map<int, int> hm;
int dup[n];
memcpy(dup, arr, n*sizeof(arr[0]));
mergeSort(dup, 0, n - 1, hm);
printf("Surpasser Count of array is \n");
for (int i = 0; i < n; i++)
printf("%d ", (n - 1) - i - hm[arr[i]]);
}
int main()
{
int arr[] = { 2, 7, 5, 3, 0, 8, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, n);
findSurpasser(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Surpasser {
public static void merge(int[] arr, int l, int m, int r, Map<Integer, Integer> hm) {
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int i = 0, j = 0, k = l;
int c = 0;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
hm.put(L[i], hm.getOrDefault(L[i], 0) + c);
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
c++;
}
}
while (i < n1) {
hm.put(L[i], hm.getOrDefault(L[i], 0) + c);
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
public static void mergeSort(int[] arr, int l, int r, Map<Integer, Integer> hm) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m, hm);
mergeSort(arr, m + 1, r, hm);
merge(arr, l, m, r, hm);
}
}
public static void findSurpasser(int[] arr,int n)
{
Map<Integer, Integer> hm = new HashMap<>();
int[] dup = arr.clone();
mergeSort(dup, 0, n - 1, hm);
System.out.println("Surpasser Count of array is: ");
for (int i = 0; i < n; i++) {
if(hm.containsKey(arr[i]))
System.out.print((n - 1) - i - hm.get(arr[i]) + " ");
else
System.out.print((n - 1) - i + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {2, 7, 5, 3, 0, 8, 1};
int n = arr.length;
System.out.println("Given array is: " + Arrays.toString(arr));
findSurpasser(arr, n);
}
}
|
Python3
def merge(arr, l, m, r, hm):
n1 = m - l + 1
n2 = r - m
L= [0 for i in range(n1)]
R = [0 for i in range(n2)]
for i in range(n1):
L[i] = arr[l + i]
for j in range(n2):
R[j] = arr[m + 1 + j]
i,j,k,c = 0,0,l,0
while (i < n1 and j < n2):
if (L[i] <= R[j]):
if(L[i] in hm):
hm[L[i]] += c
else :
hm[L[i]] = c
arr[k] = L[i]
k += 1
i += 1
else:
arr[k] = R[j]
c += 1
k += 1
j += 1
while (i < n1):
if(L[i] in hm):
hm[L[i]] += c
else :
hm[L[i]] = c
arr[k] = L[i]
k += 1
i += 1
while (j < n2):
arr[k] = R[j]
k += 1
j += 1
def mergeSort(arr,l,r,hm):
if (l < r):
m = l + (r - l) // 2
mergeSort(arr, l, m, hm)
mergeSort(arr, m + 1, r, hm)
merge(arr, l, m, r, hm)
def printArray(arr,n):
for i in range(n):
print(arr[i],end = " ")
print("")
def findSurpasser(arr, n):
hm = {}
dup = arr[:]
mergeSort(dup, 0, n - 1, hm)
print("Surpasser Count of array is ")
for i in range(n):
print((n - 1) - i - (hm[arr[i]] if arr[i] in hm else 0),end = " ")
arr = [ 2, 7, 5, 3, 0, 8, 1 ]
n = len(arr)
print("Given array is ")
printArray(arr, n)
findSurpasser(arr, n)
|
Javascript
<script>
function merge(arr, l, m, r, hm)
{
let i=0, j=0, k=0;
let n1 = m - l + 1;
let n2 = r - m;
let L=Array(n1);
let R=Array(n2);
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0, j = 0, k = l;
let c = 0;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
hm[L[i]] += c;
arr[k++] = L[i++];
}
else
{
arr[k++] = R[j++];
c++;
}
}
while (i < n1)
{
hm[L[i]] += c;
arr[k++] = L[i++];
}
while (j < n2)
arr[k++] = R[j++];
}
function mergeSort(arr, l, r, hm)
{
if (l < r)
{
let m = l + Math.floor((r - l)/2);
mergeSort(arr, l, m, hm);
mergeSort(arr, m + 1, r, hm);
merge(arr, l, m, r, hm);
}
}
function printArray(arr, n)
{
for (let i = 0; i < n; i++)
document.write(arr[i]+" ");
document.write("<br>");
}
function findSurpasser(arr, n)
{
let hm=Array(10000).fill(0);
let dup= Array(n).fill(0);
for(i=0;i<n;i++)
dup[i]=arr[i];
mergeSort(dup, 0, n - 1, hm);
document.write("Surpasser Count of array is <br>");
for (let i = 0; i < n; i++)
document.write((n - 1) - i - hm[arr[i]]+" ");
}
let arr = [ 2, 7, 5, 3, 0, 8, 1 ];
let n = arr.length;
document.write("Given array is <br>");
printArray(arr, n);
findSurpasser(arr, n);
</script>
|
C#
using System;
using System.Collections.Generic;
public class Surpasser {
public static void Merge(int[] arr, int l, int m, int r,
Dictionary<int, int> hm)
{
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
for (int x = 0; x < n1; x++) {
L[x] = arr[l + x];
}
for (int y = 0; y < n2; y++) {
R[y] = arr[m + 1 + y];
}
int i = 0, j = 0, k = l;
int c = 0;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
if (hm.ContainsKey(L[i])) {
hm[L[i]] += c;
}
else {
hm.Add(L[i], c);
}
arr[k++] = L[i++];
}
else {
arr[k++] = R[j++];
c++;
}
}
while (i < n1) {
if (hm.ContainsKey(L[i])) {
hm[L[i]] += c;
}
else {
hm.Add(L[i], c);
}
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
public static void MergeSort(int[] arr, int l, int r,
Dictionary<int, int> hm)
{
if (l < r) {
int m = l + (r - l) / 2;
MergeSort(arr, l, m, hm);
MergeSort(arr, m + 1, r, hm);
Merge(arr, l, m, r, hm);
}
}
public static void FindSurpasser(int[] arr, int n)
{
Dictionary<int, int> hm
= new Dictionary<int, int>();
int[] dup = (int[])arr.Clone();
MergeSort(dup, 0, n - 1, hm);
Console.Write("Surpasser Count of array is: \n");
for (int i = 0; i < n; i++) {
if (hm.ContainsKey(arr[i])) {
Console.Write((n - 1) - i - hm[arr[i]]
+ " ");
}
else {
Console.Write((n - 1) - i + " ");
}
}
Console.WriteLine();
}
public static void Main(string[] args)
{
int[] arr = { 2, 7, 5, 3, 0, 8, 1 };
int n = arr.Length;
Console.WriteLine("Given array is: \n"
+ string.Join(" ", arr));
FindSurpasser(arr, n);
}
}
|
Output
Given array is
2 7 5 3 0 8 1
Surpasser Count of array is
4 1 1 1 2 0 0
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...