Open In App

Count of array elements which are greater than all elements on its left

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of size n, the task is to count the number of array elements such that all the elements to its left are strictly smaller than it.

Note: The first element of the array will be considered to be always satisfying the condition.

Examples :

Input: arr[] = [2, 4, 5, 6]
Output: 4
Explanation: Since the array is in increasing order, all the array elements satisfy the condition.
Hence, the count of such elements is equal to the size of the array, i.e. equal to 4.

Input: arr[] = [2, 1, 4, 3, 5]
Output: 3
Explanation: The elements 2, 4, 5 satisfy the given condition.

Input: arr[] = [3, 3, 3, 3, 3, 3]
Output: 1
Explanation: The first array element is the only element satisfying the condition.

[Naive Approach] Using 2 Nested Loops - O(n^2) time and O(1) space

The idea is to use 2 nested loops and for each element, compare it with all the values present on its left side. If it is greater than all values, then increment the answer count.

C++
// C++ program to count of array elements which 
// are greater than all elements on its left.
#include<bits/stdc++.h>
using namespace std;

// Function to return the count of 
// array elements with all elements 
// to its left smaller than it.
int count(vector<int> &arr) {
    int n = arr.size();
    
    // Stores the count
    int count = 0;

    for (int i=0; i<n; i++) {
        
        bool isMax = true;
        
        // Compare current value with 
        // all left side values 
        for (int j=0; j<i; j++) {
            
            if (arr[i] <= arr[j]) {
                isMax = false;
                break;
            }
        }
        
        if (isMax) count++;
    }
    
    return count;
}

int main() {
    vector<int> arr = {2, 4, 5, 6};
    cout << count(arr);
    return 0;
}
Java
// Java program to count of array elements which 
// are greater than all elements on its left.
class GfG {

    // Function to return the count of 
    // array elements with all elements 
    // to its left smaller than it.
    static int count(int[] arr) {
        int n = arr.length;

        // Stores the count
        int count = 0;

        for (int i = 0; i < n; i++) {

            boolean isMax = true;

            // Compare current value with 
            // all left side values 
            for (int j = 0; j < i; j++) {

                if (arr[i] <= arr[j]) {
                    isMax = false;
                    break;
                }
            }

            if (isMax) count++;
        }

        return count;
    }

    public static void main(String[] args) {
        int[] arr = {2, 4, 5, 6};
        System.out.println(count(arr));
    }
}
Python
# Python program to count of array elements which 
# are greater than all elements on its left.

# Function to return the count of 
# array elements with all elements 
# to its left smaller than it.
def count(arr):
    n = len(arr)

    # Stores the count
    count = 0

    for i in range(n):

        isMax = True

        # Compare current value with 
        # all left side values 
        for j in range(i):

            if arr[i] <= arr[j]:
                isMax = False
                break

        if isMax:
            count += 1

    return count

if __name__ == "__main__":
    arr = [2, 4, 5, 6]
    print(count(arr))
C#
// C# program to count of array elements which 
// are greater than all elements on its left.
using System;

class GfG {

    // Function to return the count of 
    // array elements with all elements 
    // to its left smaller than it.
    static int count(int[] arr) {
        int n = arr.Length;

        // Stores the count
        int count = 0;

        for (int i = 0; i < n; i++) {

            bool isMax = true;

            // Compare current value with 
            // all left side values 
            for (int j = 0; j < i; j++) {

                if (arr[i] <= arr[j]) {
                    isMax = false;
                    break;
                }
            }

            if (isMax) count++;
        }

        return count;
    }

    static void Main(string[] args) {
        int[] arr = {2, 4, 5, 6};
        Console.WriteLine(count(arr));
    }
}
JavaScript
// JavaScript program to count of array elements which 
// are greater than all elements on its left.

// Function to return the count of 
// array elements with all elements 
// to its left smaller than it.
function count(arr) {
    let n = arr.length;

    // Stores the count
    let count = 0;

    for (let i = 0; i < n; i++) {

        let isMax = true;

        // Compare current value with 
        // all left side values 
        for (let j = 0; j < i; j++) {

            if (arr[i] <= arr[j]) {
                isMax = false;
                break;
            }
        }

        if (isMax) count++;
    }

    return count;
}

let arr = [2, 4, 5, 6];
console.log(count(arr));

Output
4

[Expected Approach] Using Single Pass - O(n) time and O(1) space

The idea is to traverse the array, starting from 1st index while keeping track of the maximum element on the left. If the current element is greater than maximum value, then increment the answer count and update the maximum value.

Step by step approach:

  1. Set count = 1, as the first array element will be considered to be satisfying the condition.
  2. Set the first array element(i.e. arr[0]) as the maximum.
  3. Traverse the array starting from i =1, and compare every array element with the current maximum.
    • If an array element is found to be greater than the current maximum, that element satisfies the condition as all the elements on its left are smaller than it. Hence, increase the count, and update the maximum.
  4. Finally, return the count.
C++
// C++ program to count of array elements which 
// are greater than all elements on its left.
#include<bits/stdc++.h>
using namespace std;

// Function to return the count of 
// array elements with all elements 
// to its left smaller than it.
int count(vector<int> &arr) {
    int n = arr.size();
    
    // Stores the count
    int count = 1;

    // Stores the maximum
    int maxi = arr[0];

    // Iterate over the array
    for(int i = 1; i < n; i++) {
        
        // If an element greater
        // then maximum is obtained
        if (arr[i] > maxi) {
            
            // Increase count
            count += 1;

            // Update maximum
            maxi = arr[i];
        }
    }
    
    return count;
}

int main() {
    vector<int> arr = {2, 4, 5, 6};
    cout << count(arr);
    return 0;
}
Java
// Java program to count of array elements which 
// are greater than all elements on its left.
class GfG {

    // Function to return the count of 
    // array elements with all elements 
    // to its left smaller than it.
    static int count(int[] arr) {
        int n = arr.length;
        
        // Stores the count
        int count = 1;

        // Stores the maximum
        int maxi = arr[0];

        // Iterate over the array
        for (int i = 1; i < n; i++) {
            
            // If an element greater
            // then maximum is obtained
            if (arr[i] > maxi) {
                
                // Increase count
                count += 1;

                // Update maximum
                maxi = arr[i];
            }
        }
        
        return count;
    }

    public static void main(String[] args) {
        int[] arr = {2, 4, 5, 6};
        System.out.println(count(arr));
    }
}
Python
# Python program to count of array elements which 
# are greater than all elements on its left.

# Function to return the count of 
# array elements with all elements 
# to its left smaller than it.
def count(arr):
    n = len(arr)

    # Stores the count
    count = 1

    # Stores the maximum
    maxi = arr[0]

    # Iterate over the array
    for i in range(1, n):

        # If an element greater
        # then maximum is obtained
        if arr[i] > maxi:

            # Increase count
            count += 1

            # Update maximum
            maxi = arr[i]

    return count

if __name__ == "__main__":
    arr = [2, 4, 5, 6]
    print(count(arr))
C#
// C# program to count of array elements which 
// are greater than all elements on its left.
using System;

class GfG {

    // Function to return the count of 
    // array elements with all elements 
    // to its left smaller than it.
    static int count(int[] arr) {
        int n = arr.Length;

        // Stores the count
        int count = 1;

        // Stores the maximum
        int maxi = arr[0];

        // Iterate over the array
        for (int i = 1; i < n; i++) {

            // If an element greater
            // then maximum is obtained
            if (arr[i] > maxi) {

                // Increase count
                count += 1;

                // Update maximum
                maxi = arr[i];
            }
        }

        return count;
    }

    static void Main(string[] args) {
        int[] arr = {2, 4, 5, 6};
        Console.WriteLine(count(arr));
    }
}
JavaScript
// JavaScript program to count of array elements which 
// are greater than all elements on its left.

// Function to return the count of 
// array elements with all elements 
// to its left smaller than it.
function count(arr) {
    let n = arr.length;

    // Stores the count
    let count = 1;

    // Stores the maximum
    let maxi = arr[0];

    // Iterate over the array
    for (let i = 1; i < n; i++) {

        // If an element greater
        // then maximum is obtained
        if (arr[i] > maxi) {

            // Increase count
            count += 1;

            // Update maximum
            maxi = arr[i];
        }
    }

    return count;
}

let arr = [2, 4, 5, 6];
console.log(count(arr));

Output
4

Similar Article:


Next Article
Article Tags :

Similar Reads