Find the nearest smaller numbers on left side in an array - GeeksforGeeks

PHOTO EMBED

Mon Jan 29 2024 08:47:51 GMT+0000 (Coordinated Universal Time)

Saved by @msagr

#include <iostream>
#include <stack>
using namespace std;
 
// Prints smaller elements on left side of every element
void printPrevSmaller(int arr[], int n)
{
    // Create an empty stack
    stack<int> S;
 
    // Traverse all array elements
    for (int i=0; i<n; i++)
    {
        // Keep removing top element from S while the top
        // element is greater than or equal to arr[i]
        while (!S.empty() && S.top() >= arr[i])
            S.pop();
 
        // If all elements in S were greater than arr[i]
        if (S.empty())
            cout << "_, ";
        else  //Else print the nearest smaller element
            cout << S.top() << ", ";
 
        // Push this element
        S.push(arr[i]);
    }
}
 
int main()
{
    int arr[] = {1, 3, 0, 2, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    printPrevSmaller(arr, n);
    return 0;
}
content_copyCOPY

nearest element in leftmost array

https://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array/