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

21 Apr, 2021
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: 4Explanation: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: { 3, 3, 3, 3, 3, 3 }Output: 1Explanation: The first array element is the only element satisfying the condition.
Approach:Follow the steps below to solve the problem:
Set count = 1, as the first array element will be considered to be satisfying the condition.
Set the first array element(i.e. arr[0]) as the maximum.
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.
Finally, print the count.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement the//above approach#include<bits/stdc++.h>using namespace std; // Function to return the count// of array elements with all// elements to its left smaller// than itint count_elements(int arr[], int n){         // Stores the count    int count = 1;     // Stores the maximum    int max = arr[0];     // Iterate over the array    for(int i = 1; i < n; i++)    {                 // If an element greater        // than maximum is obtained        if (arr[i] > max)        {                         // Increase count            count += 1;             // Update maximum            max = arr[i];        }    }    return count;} // Driver Codeint main(){    int arr[] = { 2, 1, 4, 6, 3 };    int n = sizeof(arr) / sizeof(arr[0]);         cout << (count_elements(arr, n));} // This code is contributed by chitranayal
// Java program to implement the//above approachimport java.util.*; class GFG{     // Function to return the count// of array elements with all// elements to its left smaller// than itstatic int count_elements(int arr[], int n){         // Stores the count    int count = 1;     // Stores the maximum    int max = arr[0];     // Iterate over the array    for(int i = 1; i < n; i++)    {                 // If an element greater        // than maximum is obtained        if (arr[i] > max)        {                         // Increase count            count += 1;             // Update maximum            max = arr[i];        }    }    return count;} // Driver Codepublic static void main(String s[]){    int arr[] = { 2, 1, 4, 6, 3 };    int n = arr.length;         System.out.print(count_elements(arr, n));}} // This code is contributed by rutvik_56
# Python3 Program to implement# the above approach # Function to return the count# of array elements with all# elements to its left smaller# than it  def count_elements(arr):     # Stores the count    count = 1     # Stores the maximum    max = arr[0]     # Iterate over the array    for i in range(1, len(arr)):         # If an element greater        # than maximum is obtained        if arr[i] > max:             # Increase count            count += 1             # Update maximum            max = arr[i]    return count  # Driver Codearr = [2, 1, 4, 6, 3]print(count_elements(arr))
// C# program to implement the// above approachusing System; class GFG{ // Function to return the count// of array elements with all// elements to its left smaller// than itstatic int count_elements(int[] arr, int n){         // Stores the count    int count = 1;     // Stores the maximum    int max = arr[0];     // Iterate over the array    for(int i = 1; i < n; i++)    {                 // If an element greater        // than maximum is obtained        if (arr[i] > max)        {                         // Increase count            count += 1;             // Update maximum            max = arr[i];        }    }    return count;} // Driver Codepublic static void Main(){    int[] arr = { 2, 1, 4, 6, 3 };    int n = arr.Length;     Console.Write(count_elements(arr, n));}} // This code is contributed by jrishabh99
<script> // Javascript program to implement the// above approach // Function to return the count// of array elements with all// elements to its left smaller// than itfunction count_elements(arr, n){         // Stores the count    let count = 1;     // Stores the maximum    let max = arr[0];     // Iterate over the array    for(let i = 1; i < n; i++)    {                 // If an element greater        // than maximum is obtained        if (arr[i] > max)        {                         // Increase count            count += 1;             // Update maximum            max = arr[i];        }    }    return count;} // Driver Codelet arr = [ 2, 1, 4, 6, 3 ];let n = arr.length; document.write(count_elements(arr, n)); // This code is contributed by rishavmahato348 </script>
3
  Time Complexity: O(N)Auxiliary Space: O(1) 
ukasp
rutvik_56
jrishabh99
rishavmahato348
Arrays
Greedy
Mathematical
Searching
Arrays
Searching
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Write a program to print all permutations of a given string
Huffman Coding | Greedy Algo-3