Count array elements having at least one smaller element on its left and right side

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:

  1. arr[1] (= 4) has smaller element on left as 3 and on the right as 4
  2. arr[3] (= 6) has smaller element on left as 4 and on the right as 5.
  3. 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:
  • After the above steps, the value of count gives the resultant count.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.