Count array elements exceeding all previous elements as well as the next array element
Last Updated :
31 May, 2021
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 :
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfIntegers( int arr[], int N)
{
int cur_max = 0, count = 0;
if (N == 1) {
count = 1;
}
else {
for ( int i = 0; i < N - 1; i++) {
if (arr[i] > cur_max) {
cur_max = arr[i];
if (arr[i] > arr[i + 1]) {
count++;
}
}
}
if (arr[N - 1] > cur_max)
count++;
}
cout << count;
}
int main()
{
int arr[] = { 1, 2, 0, 7, 2, 0, 2, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
numberOfIntegers(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void numberOfIntegers( int [] arr, int N)
{
int cur_max = 0 , count = 0 ;
if (N == 1 ) {
count = 1 ;
}
else
{
for ( int i = 0 ; i < N - 1 ; i++)
{
if (arr[i] > cur_max)
{
cur_max = arr[i];
if (arr[i] > arr[i + 1 ])
{
count++;
}
}
}
if (arr[N - 1 ] > cur_max)
count++;
}
System.out.println(count);
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 };
int N = arr.length;
numberOfIntegers(arr, N);
}
}
|
Python3
def numberOfIntegers(arr, N) :
cur_max = 0
count = 0
if (N = = 1 ) :
count = 1
else :
for i in range (N - 1 ):
if (arr[i] > cur_max) :
cur_max = arr[i]
if (arr[i] > arr[i + 1 ]) :
count + = 1
if (arr[N - 1 ] > cur_max) :
count + = 1
print (count)
arr = [ 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 ]
N = len (arr)
numberOfIntegers(arr, N)
|
C#
using System;
class GFG
{
static void numberOfIntegers( int [] arr, int N)
{
int cur_max = 0, count = 0;
if (N == 1) {
count = 1;
}
else {
for ( int i = 0; i < N - 1; i++) {
if (arr[i] > cur_max) {
cur_max = arr[i];
if (arr[i] > arr[i + 1]) {
count++;
}
}
}
if (arr[N - 1] > cur_max)
count++;
}
Console.WriteLine(count);
}
static public void Main()
{
int [] arr = new int [] { 1, 2, 0, 7, 2, 0, 2, 0 };
int N = arr.Length;
numberOfIntegers(arr, N);
}
}
|
Javascript
<script>
function numberOfIntegers(arr, N)
{
let cur_max = 0, count = 0;
if (N == 1) {
count = 1;
}
else
{
for (let i = 0; i < N - 1; i++)
{
if (arr[i] > cur_max)
{
cur_max = arr[i];
if (arr[i] > arr[i + 1])
{
count++;
}
}
}
if (arr[N - 1] > cur_max)
count++;
}
document.write(count);
}
let arr = [ 1, 2, 0, 7, 2, 0, 2, 0 ];
let N = arr.length;
numberOfIntegers(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...