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] (= 4) 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 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 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.
- Iterate until the stack is not empty and the current element is less than the top element of the stack then:
- After the above steps, the value of count gives the resultant count.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> #include <iostream> using namespace std; // Function to count the number of // elements that have smaller on // left and right side void findElements(int* arr, int N) { // Initialize stack stack<int> stack; // Stores the required count // of array elements int count = 0; // Traverse the array A{] for (int i = 0; i < N; i++) { // If stack is not empty // and stack top > arr[i] while (!stack.empty() && arr[i] < stack.top()) { // If stack size > 1 if (stack.size() > 1) // Increment count count++; // Pop the top element stack.pop(); } // Push the element arr[i] stack.push(arr[i]); } // Print the final count cout << count; } // Driver Code int main() { int arr[] = { 3, 9, 4, 6, 7, 5 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call findElements(arr, N); return 0; } |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
Recommended Posts:
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of smaller elements on right side of each element in an Array using Merge sort
- Generate array having differences between count of occurrences of every array element on its left and right
- Count smaller elements on right side using Set in C++ STL
- Count smaller elements on right side
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element of array with sum of elements on its right side
- Find the nearest smaller numbers on left side in an array
- Largest element smaller than current element on left for every element in Array
- Count greater elements on the left side of every array element
- Find maximum difference between nearest left and right smaller elements
- Replace every element with the least greater element on its right
- Count the number of elements which are greater than any of element on right side of an array
- Find the farthest smaller number in the right side
- Steps to make array empty by removing maximum and its right side
- Find Array formed by adding each element of given array with largest element in new array to its left
- Count of array elements which is smaller than both its adjacent elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

