# Elements before which no element is bigger in array

Given an array of integers, the task is to find count of elements before which all the elements are smaller. First element is always counted as there is no other element before it.

Examples:

``` Input :   arr[] = {10, 40, 23, 35, 50, 7}
Output :  3
The elements are 10, 40 and 50.

Input :   arr[] = {5, 4, 1}
Output :  1
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A Naive approach is to one by one consider an element and check with all the previous elements. If an element is greater than all, increment the result.

An Efficient method is to store the maximum value in the array at each index and if the next element is greater than maximum value increment the result and update maximum with that element.

Below is implementation of this method.

## C++

 `// C++ program to find elements that are greater than all ` `// previous elements ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count elements that are greater than all ` `// previous elements ` `int` `countElements(``int` `arr[], ``int` `n) ` `{ ` `    ``// First element will always be considered as greater ` `    ``// than previous ones ` `    ``int` `result = 1; ` ` `  `    ``// Store the arr as maximum ` `    ``int` `max_ele = arr; ` ` `  `    ``// Traverse array starting from second element ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``// Compare current element with the maximum ` `        ``// value if it is true otherwise continue ` `        ``if` `(arr[i] > max_ele) { ` `            ``// Update the maximum value ` `            ``max_ele = arr[i]; ` ` `  `            ``// Increment the result ` `            ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 10, 40, 23, 35, 50, 7 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << countElements(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find elements that are greater than all ` `// previous elements ` ` `  `class` `Test { ` `    ``// Method to count elements that are greater than all ` `    ``// previous elements ` `    ``static` `int` `countElements(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// First element will always be considered as greater ` `        ``// than previous ones ` `        ``int` `result = ``1``; ` ` `  `        ``// Store the arr as maximum ` `        ``int` `max_ele = arr[``0``]; ` ` `  `        ``// Traverse array starting from second element ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``// Compare current element with the maximum ` `            ``// value if it is true otherwise continue ` `            ``if` `(arr[i] > max_ele) { ` `                ``// Update the maximum value ` `                ``max_ele = arr[i]; ` ` `  `                ``// Increment the result ` `                ``result++; ` `            ``} ` `        ``} ` ` `  `        ``return` `result; ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``10``, ``40``, ``23``, ``35``, ``50``, ``7` `}; ` `        ``System.out.println(countElements(arr, arr.length)); ` `    ``} ` `} `

## Python 3

 `# Python 3 program to find  ` `# elements that are greater  ` `# than all previous elements ` ` `  `# Function to count elements ` `# that are greater than all ` `# previous elements ` `def` `countElements(arr, n): ` ` `  `    ``# First element will always  ` `    ``# be considered as greater ` `    ``# than previous ones ` `    ``result ``=` `1` ` `  `    ``# Store the arr ` `    ``# as maximum ` `    ``max_ele ``=` `arr[``0``] ` ` `  `    ``# Traverse array starting ` `    ``# from second element ` `    ``for` `i ``in` `range``(``1``, n ):  ` `        ``# Compare current element ` `        ``# with the maximum ` `        ``# value if it is true ` `        ``# otherwise continue ` `        ``if` `(arr[i] > max_ele): ` `             `  `            ``# Update the ` `            ``# maximum value ` `            ``max_ele ``=` `arr[i] ` ` `  `            ``# Increment ` `            ``# the result ` `            ``result ``+``=` `1` ` `  `    ``return` `result ` ` `  `# Driver code ` `arr ``=` `[``10``, ``40``, ``23``,  ` `       ``35``, ``50``, ``7``] ` `n ``=` `len``(arr) ` `print``(countElements(arr, n)) ` ` `  `# This code is contributed ` `# by Smitha `

## C#

 `// C# program to find elements that  ` `// are greater than all previous elements ` `using` `System; ` ` `  `class` `GFG {  ` `     `  `    ``// Method to count elements that are  ` `    ``// greater than all previous elements ` `    ``static` `int` `countElements(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``// First element will always be considered ` `        ``// as greater than previous ones ` `        ``int` `result = 1; ` ` `  `        ``// Store the arr as maximum ` `        ``int` `max_ele = arr; ` ` `  `        ``// Traverse array starting from second element ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `             `  `            ``// Compare current element with the maximum ` `            ``// value if it is true otherwise continue ` `            ``if` `(arr[i] > max_ele) { ` `                 `  `                ``// Update the maximum value ` `                ``max_ele = arr[i]; ` ` `  `                ``// Increment the result ` `                ``result++; ` `            ``} ` `        ``} ` ` `  `        ``return` `result; ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 10, 40, 23, 35, 50, 7 }; ` `        ``Console.WriteLine(countElements(arr, arr.Length)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` ``\$max_ele``)  ` `        ``{ ` `             `  `            ``// Update the maximum value ` `            ``\$max_ele` `= ``\$arr``[``\$i``]; ` ` `  `            ``// Increment the result ` `            ``\$result``++; ` `        ``} ` `    ``} ` ` `  `    ``return` `\$result``; ` `} ` ` `  `    ``// Driver code ` `    ``\$arr` `= ``array``(10, 40, 23, 35, 50, 7); ` `    ``\$n` `= sizeof(``\$arr``); ` `    ``echo` `countElements(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by nitin mittal.  ` `?> `

Output:

```3
```

Time Complexity : O(n) where n is number of elements in input.
Auxiliary Space : O(1)

This article is contributed by Sahil Chhabra. 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.

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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.