Problem
Section titled “Problem”Given an array, find the nearest smaller element G[i] for every element A[i] in the array such that the element has an index smaller than i.
More formally,
G[i] for an element A[i] = an element A[j] such that j is maximum possible AND j < i AND A[j] < A[i]Elements for which no smaller element exist, consider next smaller element as -1.
Complete the function vector<int> Solution::prevSmaller(vector<int> &A), which returns the array G such that G[i] contains the nearest smaller number than A[i]. If no such element occurs, G[i] should be -1.
Example 1:
Input: A = [4, 5, 2, 10, 8]Output: G = [-1, 4, -1, 2, 2]Explanation:index 1: No element less than 4 in left of 4, G[1] = -1index 2: A[1] is only element less than A[2], G[2] = A[1]index 3: No element less than 2 in left of 2, G[3] = -1index 4: A[3] is nearest element which is less than A[4], G[4] = A[3]index 5: A[3] is nearest element which is less than A[5], G[5] = A[3](The explanation uses 1-based indices.)
Example 2:
Input: A = [3, 2, 1]Output: [-1, -1, -1]Explanation:index 1: No element less than 3 in left of 3, G[1] = -1index 2: No element less than 2 in left of 2, G[2] = -1index 3: No element less than 1 in left of 1, G[3] = -1Constraints (assumed): the source does not list any. The solution below is linear and is designed for
- every
A[i]fits in a 32-bit signed integer.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Scanning left from every index takes in the worst case (think of an increasing array followed by a tiny value). The key observation is that many elements can never be an answer again:
if
j < kandA[j] >= A[k], thenA[j]is never the nearest smaller element for any index afterk—A[k]is closer and at least as small, so it would always be preferred (or, ifA[k]is not smaller, neither isA[j]).
So, at any moment, the only useful candidates are the elements to the left that are strictly smaller than everything to their right. Read from left to right they form a strictly increasing sequence, which we keep in a stack st.
For a new value A[i]: pop every candidate >= A[i] (they are not smaller than A[i], and by the observation above A[i] now makes them useless for the future too). Whatever remains on top is the nearest element to the left that is smaller than A[i], or the stack is empty and the answer is -1. Then push A[i], which keeps the stack increasing.
Algorithm
Section titled “Algorithm”- Start with an empty stack
stand an answer arrayGof sizen. - For each
ifrom0ton - 1:- while
stis non-empty andst.top() >= A[i], pop; G[i] = st.empty() ? -1 : st.top();- push
A[i].
- while
- Return
G.
Walkthrough
Section titled “Walkthrough”Example 1. The blue cell is A[i]; the stack line lists the candidates from bottom to top, and G the answers so far:
Complexity
Section titled “Complexity”- Time: — every element is pushed once and popped at most once, so the inner
whileruns at mostntimes over the whole scan. - Space: — the stack (an increasing array keeps every element) plus the output.
Edge cases
Section titled “Edge cases”- Equal values (
[2, 2]): the earlier2is popped because>=is used, so the second2gets-1— the answer must be strictly smaller. - Decreasing array (Example 2): every element pops the previous one and gets
-1. - Negative values such as
-1: emptiness of the stack, not a-1sentinel value, decides the “no smaller element” case.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
vector<int> Solution::prevSmaller(vector<int> &A) { int n = A.size(); vector<int> G(n); stack<int> st; // strictly increasing from bottom to top for (int i = 0; i < n; i++) { while (!st.empty() && st.top() >= A[i]) st.pop(); G[i] = st.empty() ? -1 : st.top(); st.push(A[i]); } return G;}public class Solution { public ArrayList<Integer> prevSmaller(List<Integer> A) { ArrayList<Integer> G = new ArrayList<>(A.size()); Deque<Integer> st = new ArrayDeque<>(); // strictly increasing from bottom to top for (int x : A) { while (!st.isEmpty() && st.peek() >= x) st.pop(); G.add(st.isEmpty() ? -1 : st.peek()); st.push(x); } return G; }}class Solution: def prevSmaller(self, A: list[int]) -> list[int]: G = [] st = [] # strictly increasing from bottom to top for x in A: while st and st[-1] >= x: st.pop() G.append(st[-1] if st else -1) st.append(x) return GGive it a try first
Attempt the problem on InterviewBit — the code will still be here.
![Monotonic stack on [4, 5, 2, 10, 8], step 1 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/01.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 2 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/02.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 3 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/03.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 4 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/04.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 5 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/05.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 6 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/06.webp)
![Monotonic stack on [4, 5, 2, 10, 8], step 7 of 7](/CodePrime/img/codes/nearest-smaller-element/stack/07.webp)