Count array elements having at least one smaller element on its left and right side
Last Updated :
26 May, 2022
Given an array arr[] of length N, the task is to find the number of elements in array arr[] which contains at least one smaller element on its left and right.
Examples:
Input: arr[] = {3, 9, 4, 6, 7, 5}
Output: 3
Explanation: Following 3 array elements satisfy the necessary conditions:
- arr[1] (= 9) has smaller element on left as 3 and on the right as 4
- arr[3] (= 6) has smaller element on left as 4 and on the right as 5.
- arr[4] (= 7) has smaller element on left as 6 and on the right as 5.
Input: arr[] = {3, 9, 14, 61, 17, 5, 12, 9, 15}
Output: 5
Naive Approach: The simplest approach is to traverse the given array and for each element, count the number of smaller elements both on its left and right. If both the counts are found to be at least 1, increase the answer by 1. Finally, print the answer obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use a Stack. Maintain an increasing stack of elements to count the smaller element in constant time. Follow the steps below to solve the problem:
- Initialize a stack and a variable count as 0 as a count of numbers that satisfy the given condition.
- Traverse the given array using the variable i and perform the following steps:
- Iterate until the stack is not empty and the current element is less than the top element of the stack then:
- The element on top of the stack has a smaller element on right i.e., arr[i].
- If the stack size is greater than 1, then there is also a smaller element on left, as the stack has been maintained as an increasing stack.
- If the above conditions are true, then increment the count by 1.
- Pop the top element from the stack.
- Push the current element into the stack.
- After the above steps, the value of count gives the resultant count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void findElements( int * arr, int N)
{
stack< int > stack;
int count = 0;
for ( int i = 0; i < N; i++) {
while (!stack.empty()
&& arr[i] < stack.top()) {
if (stack.size() > 1)
count++;
stack.pop();
}
stack.push(arr[i]);
}
cout << count;
}
int main()
{
int arr[] = { 3, 9, 4, 6, 7, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
findElements(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findElements( int [] arr,
int N)
{
Stack<Integer> stack = new Stack<>();
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
while (!stack.isEmpty() &&
arr[i] < stack.peek())
{
if (stack.size() > 1 )
count++;
stack.pop();
}
stack.add(arr[i]);
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 3 , 9 , 4 , 6 , 7 , 5 };
int N = arr.length;
findElements(arr, N);
}
}
|
Python3
def findElements(arr, N):
stack = []
count = 0
for i in range (N):
while ( len (stack) > 0 and
arr[i] < stack[ - 1 ]):
if ( len (stack) > 1 ):
count + = 1
del stack[ - 1 ]
stack.append(arr[i])
print (count)
if __name__ = = '__main__' :
arr = [ 3 , 9 , 4 , 6 , 7 , 5 ]
N = len (arr)
findElements(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findElements( int [] arr, int N)
{
Stack< int > stack = new Stack< int >();
int count = 0;
for ( int i = 0; i < N; i++)
{
while (stack.Count != 0 &&
arr[i] < stack.Peek())
{
if (stack.Count > 1)
count++;
stack.Pop();
}
stack.Push(arr[i]);
}
Console.Write(count);
}
public static void Main(String[] args)
{
int []arr = { 3, 9, 4, 6, 7, 5 };
int N = arr.Length;
findElements(arr, N);
}
}
|
Javascript
<script>
function findElements(arr, N)
{
var stack = [];
var count = 0;
for ( var i = 0; i < N; i++) {
while (stack.length!=0
&& arr[i] < stack[stack.length-1]) {
if (stack.length > 1)
count++;
stack.pop();
}
stack.push(arr[i]);
}
document.write( count);
}
var arr = [3, 9, 4, 6, 7, 5];
var N = arr.length;
findElements(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...