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*, and**count***update the***maximum.** - Finally, print the
**count.**

Below is the implementation of the above approach:

## C++

`// 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 it ` `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 Code ` `int` `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 ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement the ` `//above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the count ` `// of array elements with all ` `// elements to its left smaller ` `// than it ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 Code ` `arr ` `=` `[` `2` `, ` `1` `, ` `4` `, ` `6` `, ` `3` `] ` `print` `(count_elements(arr)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement the ` `// above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the count ` `// of array elements with all ` `// elements to its left smaller ` `// than it ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find the element before which all the elements are smaller than it, and after which all are greater
- Maximize the number of indices such that element is greater than element to its left
- Sum of all array elements less than X and greater than Y for Q queries
- Largest number not greater than N which can become prime after rearranging its digits
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count numbers with difference between number and its digit sum greater than specific value
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Largest number not greater than N all the digits of which are odd
- Count elements such that there are exactly X elements with values greater than or equal to X
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Queries to count array elements greater than or equal to a given number with updates
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Generate Array whose difference of each element with its left yields the given Array
- Node having maximum number of nodes less than its value in its subtree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.