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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find elements that are greater than all
// previous elements
#include <bits/stdc++.h>
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[0] 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 code
int main()
{
    int arr[] = { 10, 40, 23, 35, 50, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countElements(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[0] 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));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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[0]
    # 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[0] 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()
    {
        int[] arr = { 10, 40, 23, 35, 50, 7 };
        Console.WriteLine(countElements(arr, arr.Length));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find elements that
// are greater than all previous 
// elements
  
// Function to count elements that 
// are greater than all previous
// elements
function countElements($arr, $n)
{
      
    // First element will always
    // be considered as greater
    // than previous ones
    $result = 1;
  
    // Store the arr[0] 
    // as maximum
    $max_ele = $arr[0];
  
    // Traverse array starting
    // from second element
    for($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
    $arr = array(10, 40, 23, 35, 50, 7);
    $n = sizeof($arr);
    echo countElements($arr, $n);
  
// This code is contributed by nitin mittal. 
?>

chevron_right



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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



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.