Skip to content
Related Articles

Related Articles

Improve Article

Elements before which no element is bigger in array

  • Difficulty Level : Basic
  • Last Updated : 13 May, 2021

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 <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;
}

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

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

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

PHP




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

Javascript




<script>
    // Javascript program to find elements that
    // are greater than all previous elements
     
    // Method 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
        let result = 1;
   
        // Store the arr[0] as maximum
        let max_ele = arr[0];
   
        // Traverse array starting from second element
        for (let 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;
    }
     
    let arr = [ 10, 40, 23, 35, 50, 7 ];
      document.write(countElements(arr, arr.length));
     
</script>

Output:  

3

Time Complexity : O(n) where n is a 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 write.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.
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :