Count of array elements which are greater than all elements on its left
Last Updated :
17 Apr, 2025
Given an array arr[] of size n, the task is to count the number of array elements such that all the elements to its left are strictly smaller than it.
Note: The first element of the array will be considered to be always satisfying the condition.
Examples :
Input: arr[] = [2, 4, 5, 6]
Output: 4
Explanation: Since the array is in increasing order, all the array elements satisfy the condition.
Hence, the count of such elements is equal to the size of the array, i.e. equal to 4.
Input: arr[] = [2, 1, 4, 3, 5]
Output: 3
Explanation: The elements 2, 4, 5 satisfy the given condition.
Input: arr[] = [3, 3, 3, 3, 3, 3]
Output: 1
Explanation: The first array element is the only element satisfying the condition.
[Naive Approach] Using 2 Nested Loops - O(n^2) time and O(1) space
The idea is to use 2 nested loops and for each element, compare it with all the values present on its left side. If it is greater than all values, then increment the answer count.
C++
// C++ program to count of array elements which
// are greater than all elements on its left.
#include<bits/stdc++.h>
using namespace std;
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
int count(vector<int> &arr) {
int n = arr.size();
// Stores the count
int count = 0;
for (int i=0; i<n; i++) {
bool isMax = true;
// Compare current value with
// all left side values
for (int j=0; j<i; j++) {
if (arr[i] <= arr[j]) {
isMax = false;
break;
}
}
if (isMax) count++;
}
return count;
}
int main() {
vector<int> arr = {2, 4, 5, 6};
cout << count(arr);
return 0;
}
Java
// Java program to count of array elements which
// are greater than all elements on its left.
class GfG {
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
static int count(int[] arr) {
int n = arr.length;
// Stores the count
int count = 0;
for (int i = 0; i < n; i++) {
boolean isMax = true;
// Compare current value with
// all left side values
for (int j = 0; j < i; j++) {
if (arr[i] <= arr[j]) {
isMax = false;
break;
}
}
if (isMax) count++;
}
return count;
}
public static void main(String[] args) {
int[] arr = {2, 4, 5, 6};
System.out.println(count(arr));
}
}
Python
# Python program to count of array elements which
# are greater than all elements on its left.
# Function to return the count of
# array elements with all elements
# to its left smaller than it.
def count(arr):
n = len(arr)
# Stores the count
count = 0
for i in range(n):
isMax = True
# Compare current value with
# all left side values
for j in range(i):
if arr[i] <= arr[j]:
isMax = False
break
if isMax:
count += 1
return count
if __name__ == "__main__":
arr = [2, 4, 5, 6]
print(count(arr))
C#
// C# program to count of array elements which
// are greater than all elements on its left.
using System;
class GfG {
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
static int count(int[] arr) {
int n = arr.Length;
// Stores the count
int count = 0;
for (int i = 0; i < n; i++) {
bool isMax = true;
// Compare current value with
// all left side values
for (int j = 0; j < i; j++) {
if (arr[i] <= arr[j]) {
isMax = false;
break;
}
}
if (isMax) count++;
}
return count;
}
static void Main(string[] args) {
int[] arr = {2, 4, 5, 6};
Console.WriteLine(count(arr));
}
}
JavaScript
// JavaScript program to count of array elements which
// are greater than all elements on its left.
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
function count(arr) {
let n = arr.length;
// Stores the count
let count = 0;
for (let i = 0; i < n; i++) {
let isMax = true;
// Compare current value with
// all left side values
for (let j = 0; j < i; j++) {
if (arr[i] <= arr[j]) {
isMax = false;
break;
}
}
if (isMax) count++;
}
return count;
}
let arr = [2, 4, 5, 6];
console.log(count(arr));
[Expected Approach] Using Single Pass - O(n) time and O(1) space
The idea is to traverse the array, starting from 1st index while keeping track of the maximum element on the left. If the current element is greater than maximum value, then increment the answer count and update the maximum value.
Step by step approach:
- Set count = 1, as the first array element will be considered to be satisfying the condition.
- Set the first array element(i.e. arr[0]) as the maximum.
- Traverse the array starting from i =1, and compare every array element with the current maximum.
- If an array element is found to be greater than the current maximum, that element satisfies the condition as all the elements on its left are smaller than it. Hence, increase the count, and update the maximum.
- Finally, return the count.
C++
// C++ program to count of array elements which
// are greater than all elements on its left.
#include<bits/stdc++.h>
using namespace std;
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
int count(vector<int> &arr) {
int n = arr.size();
// Stores the count
int count = 1;
// Stores the maximum
int maxi = arr[0];
// Iterate over the array
for(int i = 1; i < n; i++) {
// If an element greater
// then maximum is obtained
if (arr[i] > maxi) {
// Increase count
count += 1;
// Update maximum
maxi = arr[i];
}
}
return count;
}
int main() {
vector<int> arr = {2, 4, 5, 6};
cout << count(arr);
return 0;
}
Java
// Java program to count of array elements which
// are greater than all elements on its left.
class GfG {
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
static int count(int[] arr) {
int n = arr.length;
// Stores the count
int count = 1;
// Stores the maximum
int maxi = arr[0];
// Iterate over the array
for (int i = 1; i < n; i++) {
// If an element greater
// then maximum is obtained
if (arr[i] > maxi) {
// Increase count
count += 1;
// Update maximum
maxi = arr[i];
}
}
return count;
}
public static void main(String[] args) {
int[] arr = {2, 4, 5, 6};
System.out.println(count(arr));
}
}
Python
# Python program to count of array elements which
# are greater than all elements on its left.
# Function to return the count of
# array elements with all elements
# to its left smaller than it.
def count(arr):
n = len(arr)
# Stores the count
count = 1
# Stores the maximum
maxi = arr[0]
# Iterate over the array
for i in range(1, n):
# If an element greater
# then maximum is obtained
if arr[i] > maxi:
# Increase count
count += 1
# Update maximum
maxi = arr[i]
return count
if __name__ == "__main__":
arr = [2, 4, 5, 6]
print(count(arr))
C#
// C# program to count of array elements which
// are greater than all elements on its left.
using System;
class GfG {
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
static int count(int[] arr) {
int n = arr.Length;
// Stores the count
int count = 1;
// Stores the maximum
int maxi = arr[0];
// Iterate over the array
for (int i = 1; i < n; i++) {
// If an element greater
// then maximum is obtained
if (arr[i] > maxi) {
// Increase count
count += 1;
// Update maximum
maxi = arr[i];
}
}
return count;
}
static void Main(string[] args) {
int[] arr = {2, 4, 5, 6};
Console.WriteLine(count(arr));
}
}
JavaScript
// JavaScript program to count of array elements which
// are greater than all elements on its left.
// Function to return the count of
// array elements with all elements
// to its left smaller than it.
function count(arr) {
let n = arr.length;
// Stores the count
let count = 1;
// Stores the maximum
let maxi = arr[0];
// Iterate over the array
for (let i = 1; i < n; i++) {
// If an element greater
// then maximum is obtained
if (arr[i] > maxi) {
// Increase count
count += 1;
// Update maximum
maxi = arr[i];
}
}
return count;
}
let arr = [2, 4, 5, 6];
console.log(count(arr));
Similar Article:
Similar Reads
Count elements that are greater than at least K elements on its left and right Given an array arr[] of size n (1 <= n <= 10^5) and a positive integer k, the task is to count all indices i ( 1<= i < n) in the array such that at least k elements to the left of i and at least k elements to the right of i, which are strictly smaller than the value at the ith index (i.e
10 min read
Count of Array elements greater than all elements on its left and next K elements on its right Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the
14 min read
Count greater elements on the left side of every array element Given an array arr[] of distinct integers of size N, the task is to print the count of greater elements on the left side of each array element. Examples : Input: arr[] = {12, 1, 2, 3, 0, }Output: 0 1 1 1 4Explanation:For index 0, no greater element exists on the left side.For index 1, {12} is greate
7 min read
Count of Array elements greater than all elements on its left and at least K elements on its right Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The o
15+ min read
Find all elements in array which have at-least two greater elements Given an array of n distinct elements, the task is to find all elements in array which have at-least two greater elements than themselves. Examples : Input : arr[] = {2, 8, 7, 1, 5};Output : 2 1 5 Explanation:The output three elements have two or more greater elements Explanation:Input : arr[] = {7,
11 min read