# Elements before which no element is bigger in array

Given an array of integers, the task is to find the count of elements before which all the elements are smaller. The 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 ```

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.  ` `?> `

## Javascript

 ``

Output

`3`

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

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next