Find Surpasser Count of each element in array in C++

Suppose one array A is given. We have to find a number of surpasser of each element in that array. The surpassers are greater elements which are present at the right side of the array of the current element. Suppose A = {2, 7, 5, 3, 0, 8, 1}, the surpassers are {4, 1, 1, 1, 2, 0, 0}, so 2 has 4 numbers at right side, which are greater than 4, and the same rule for others. The solution is very simple, two nested loops will be there, for each element, it will count surpassers, then store them into another array.

Example

 Live Demo

#include 
using namespace std;
void gerSurpassers(int arr[], int surpassers[], int n){
   for(int i = 0; i arr[i])
            count++;
      }
      surpassers[i] = count;
   }
}
void displayArray(int arr[], int n){
   for(int i = 0; i

Output

Elements :2 7 5 3 0 8 1
Surpassers:4 1 1 1 2 0 0
Updated on: 2019-10-21T08:07:00+05:30

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements