Open In App

Count the number of elements which are greater than any of element on right side of an array

Last Updated : 04 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array Arr[]. The task is to count the number of elements Arr[i] in the given array such that one or more smaller elements are present on the right side of the element Arr[i] in array.

Examples:  

Input: Arr[] = { 3, 9, 4, 6, 7, 5 } 
Output: 3
Numbers that counts are: 9, 6, 7 
9 – As all numbers are present after 9 are smaller than 9, 
6 – 5 is smaller element present after it, 
7 – 5 is smaller element which is present after it.

Input: Arr[] = { 3, 2, 1, 2, 3, 4, 5 } 
Output:
 

Approach: 
Start traversing array from the last till first element of the array. While traversing maintain a min variable which stores the minimum element till now and a counter variable. Compare min variable with the current element. If min variable is smaller than current element Arr[i], increase the counter and if min is greater than Arr[i] then update the min.

Below is the implementation of the above approach:  

C++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
 
// function to return the count
int count_greater(int arr[], int n)
{
    int min = INT_MAX;
    int counter = 0;
 
    // Comparing the given element
    // with minimum element till
    // occurred till now.
    for (int i = n - 1; i >= 0; i--) {
        if (arr[i] > min) {
            counter++;
        }
 
        // Updating the min variable
        if (arr[i] <= min) {
            min = arr[i];
        }
    }
 
    return counter;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 2, 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << count_greater(arr, n) << endl;
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
// function to return the count
static int count_greater(int arr[], int n)
{
    int min = Integer.MAX_VALUE;
    int counter = 0;
 
    // Comparing the given element
    // with minimum element till
    // occurred till now.
    for (int i = n - 1; i >= 0; i--)
    {
        if (arr[i] > min)
        {
            counter++;
        }
 
        // Updating the min variable
        if (arr[i] <= min)
        {
            min = arr[i];
        }
    }
    return counter;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 1, 2, 3, 4, 5 };
    int n = arr.length;
 
    System.out.println(count_greater(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation for the above approach
import sys
 
# function to return the count
def count_greater(arr, n) :
 
    min = sys.maxsize;
    counter = 0;
 
    # Comparing the given element
    # with minimum element till
    # occurred till now.
    for i in range(n - 1, -1, -1) :
        if (arr[i] > min) :
            counter += 1;
     
        # Updating the min variable
        if (arr[i] <= min) :
            min = arr[i];
 
    return counter;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 3, 2, 1, 2, 3, 4, 5 ];
    n = len(arr);
 
    print(count_greater(arr, n));
     
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// function to return the count
static int count_greater(int []arr, int n)
{
    int min = int.MaxValue;
    int counter = 0;
 
    // Comparing the given element
    // with minimum element till
    // occurred till now.
    for (int i = n - 1; i >= 0; i--)
    {
        if (arr[i] > min)
        {
            counter++;
        }
 
        // Updating the min variable
        if (arr[i] <= min)
        {
            min = arr[i];
        }
    }
    return counter;
}
 
// Driver code
static public void Main ()
{
    int []arr = { 3, 2, 1, 2, 3, 4, 5 };
    int n = arr.Length;
     
    Console.Write(count_greater(arr, n));
}
}
 
// This code is contributed by ajit.


Javascript




<script>
 
// Javascript implementation
 
// Function to return the count
function count_greater(arr, n)
{
    let min = Number.MAX_VALUE;
    let counter = 0;
 
    // Comparing the given element
    // with minimum element till
    // occurred till now.
    for(let i = n - 1; i >= 0; i--)
    {
        if (arr[i] > min)
        {
            counter++;
        }
 
        // Updating the min variable
        if (arr[i] <= min)
        {
            min = arr[i];
        }
    }
    return counter;
}
 
// Driver code
let arr = [ 3, 2, 1, 2, 3, 4, 5 ];
let n = arr.length;
 
document.write(count_greater(arr, n) + "<br>");
 
// This code is contributed by rishavmahato348
 
</script>


Output: 

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

Count smaller elements on right side and greater elements on left side using Binary Index Tree
Given an array arr[] of size N. The task is to find smaller elements on the right side and greater elements on the left side for each element arr[i] in the given array. Examples: Input: arr[] = {12, 1, 2, 3, 0, 11, 4} Output: Smaller right: 6 1 1 1 0 1 0 Greater left: 0 1 1 1 4 1 2 Input: arr[] = {5, 4, 3, 2, 1} Output: Smaller right: 4 3 2 1 0 Gre
16 min read
Length of longest subarray in which elements greater than K are more than elements not greater than K
Given an array arr[] of length N. The task is to find the length of the longest subarray in which elements greater than a given number K are more than elements not greater than K.Examples: Input : N = 5, K = 2, arr[]={ 1, 2, 3, 4, 1 } Output : 3 The subarray [2, 3, 4] or [3, 4, 1] satisfy the given condition, and there is no subarray of length 4 or
10 min read
Generate an array consisting of most frequent greater elements present on the right side of each array element
Given an array A[] of size N, the task is to generate an array B[] based on the following conditions: For every array element A[i], find the most frequent element greater than A[i] present on the right of A[i]. Insert that element into B[].If more than one such element is present on the right, choose the element having the smallest value.If no elem
9 min read
Count of Array elements greater than all elements on its left and next K elements on its right
Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the array and greater than its next K(= 2) elements {2
14 min read
C++ Program to Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
9 min read
Java Program to Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
7 min read
Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
29 min read
Find the element before which all the elements are smaller than it, and after which all are greater
Given an array, find an element before which all elements are smaller than it, and after which all are greater than it. Return the index of the element if there is such an element, otherwise, return -1. Examples: Input: arr[] = {5, 1, 4, 3, 6, 8, 10, 7, 9}; Output: 4 Explanation: All elements on left of arr[4] are smaller than it and all elements o
23 min read
Delete nodes which have a greater value on right side using recursion
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 15-&gt;11-&gt;6-&gt;3-&gt;NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see
7 min read
Delete nodes which have a greater value on right side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 15-&gt;11-&gt;6-&gt;3-&gt;NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see
19 min read