07. Next Greater Element in Circular Array
β GFG solution to the Next Greater Element in Circular Array problem: find the next greater element for each element in a circular array using monotonic stack technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a circular integer array arr[]
, the task is to determine the next greater element (NGE) for each element in the array.
The next greater element of an element arr[i]
is the first element that is greater than arr[i]
when traversing circularly. If no such element exists, return -1 for that position.
Circular Property:
Since the array is circular, after reaching the last element, the search continues from the beginning until we have looked at all elements once.
π Examples
Example 1
Input: arr[] = [1, 3, 2, 4]
Output: [3, 4, 4, -1]
Explanation:
The next greater element for 1 is 3.
The next greater element for 3 is 4.
The next greater element for 2 is 4.
The next greater element for 4 does not exist, so return -1.
Example 2
Input: arr[] = [0, 2, 3, 1, 1]
Output: [2, 3, -1, 2, 2]
Explanation:
The next greater element for 0 is 2.
The next greater element for 2 is 3.
The next greater element for 3 does not exist, so return -1.
The next greater element for 1 is 2 (from circular traversal).
The next greater element for 1 is 2 (from circular traversal).
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i] \le 10^6$
β
My Approach
The optimal solution leverages a Monotonic Stack combined with a Circular Traversal to efficiently compute the Next Greater Element for each position in the array:
Monotonic Stack with Circular Traversal
Problem Observation:
We're asked to find the next greater element for every element in the array, considering the array as circular.
If no greater element exists, return
-1
for that position.
Circular Traversal Strategy:
To simulate a circular array, we traverse the array twice (from
2n-1
to0
), using modulo (% n
) to wrap around.This ensures every element can βseeβ all others to its right and left (as per circular property).
Stack Stores Candidates:
Use a monotonic decreasing stack to keep track of potential "next greater" elements.
Elements in the stack are always greater than the current value for which we are finding the NGE.
Processing Each Index (Right to Left):
For every index
i
from2n-1
to0
:Discard all elements from the stack that are less than or equal to
arr[i % n]
(they can't be NGE).If we're in the first pass (i < n):
The top of the stack is the next greater element for this index.
If the stack is empty, it means no NGE exists; keep result as
-1
.
Push
arr[i % n]
into the stack for future comparisons.
Result Accumulation:
Maintain a
res[]
array initialized to-1
.Update the result during the first pass only, to avoid overwriting values from the second simulated pass.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. Each element is pushed and popped from the stack at most once during the 2*n traversal.
Expected Auxiliary Space Complexity: O(n), for the stack that stores at most n elements in the worst case.
π§βπ» Code (C++)
class Solution {
public:
vector<int> nextLargerElement(vector<int>& arr) {
int n = arr.size();
vector<int> res(n, -1);
stack<int> st;
for (int i = 2 * n - 1; i >= 0; i--) {
while (!st.empty() && st.top() <= arr[i % n]) st.pop();
if (i < n && !st.empty()) res[i] = st.top();
st.push(arr[i % n]);
}
return res;
}
};
π§βπ» Code (Java)
class Solution {
public ArrayList<Integer> nextLargerElement(int[] arr) {
int n = arr.length;
ArrayList<Integer> res = new ArrayList<>(Collections.nCopies(n, -1));
Deque<Integer> st = new ArrayDeque<>();
for (int i = 2 * n - 1; i >= 0; i--) {
while (!st.isEmpty() && st.peek() <= arr[i % n]) st.pop();
if (i < n && !st.isEmpty()) res.set(i, st.peek());
st.push(arr[i % n]);
}
return res;
}
}
π Code (Python)
class Solution:
def nextLargerElement(self, arr):
n = len(arr)
res = [-1] * n
st = []
for i in range(2 * n - 1, -1, -1):
while st and st[-1] <= arr[i % n]:
st.pop()
if i < n and st:
res[i] = st[-1]
st.append(arr[i % n])
return res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated