VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-array-elements-exceeding-all-previous-elements-as-well-as-the-next-array-element/

⇱ Count array elements exceeding all previous elements as well as the next array element - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count array elements exceeding all previous elements as well as the next array element

Last Updated : 23 Jul, 2025

Given an array arr[], the task is to find the count of array elements satisfying the following conditions:

  • The array elements should be strictly greater than all the previously occurring array elements.
  • Either it is the last array element or the integer should be strictly larger than the next array element.

Note: The first integer of the array can also be considered.

Examples:

Input: arr[] = {1, 2, 0, 7, 2, 0, 2, 0}
Output: 2
Explanation: arr[1] (= 2) and arr[3] ( = 7) are the array elements satisfying the given condition.

Input: arr[] = {4, 8, 15, 16, 23, 42}
Output: 1

Approach: The idea is to linearly traverse the array and check for each array element, if it satisfies the given condition or not. Follow the steps below to solve this problem:

  • Traverse the array.
  • Starting from the first element of the array, keep track of the maximum array element encountered so far.
  • Update the maximum array element if it's greater than the previous maximum array element encountered.
  • After updating the current maximum, check if the next array element is greater than the current array element or not. If found to be true, increment the count.
  • Repeat this process until the last array element is traversed.
  • Finally, print the count obtained.

Below is the implementation for the above approach :


Output: 
2

 

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

Comment
Article Tags:
Article Tags: