All the test cases except the first test case are passed. Please help me identify as to why the first test case is not getting passed?
LINK : https://ide.codingblocks.com/s/314440
Find the next greater element
Kindly help me as soonas possible.
@gptradhika1208
Consider a test case:
5
7 3 4 5 2
correct output : -1 4 5 7 7
your code gives : -1 4 5 -1 -1
How am I supposed to correct it?
I tried it using another approach. However it is still not giving the desired output. LINK : https://ide.codingblocks.com/s/315881
Kindly reply to my query
@gptradhika1208
you can refer to this logic
vector<int> nextGreaterElements(vector<int> &nums)
{
int n = nums.size();
vector<int> res(n);
stack<int> s;
for (int i = 2 * n - 1; i >= 0; i--)
{
while (!s.empty() && nums[s.top()] <= nums[i % n])
{
s.pop();
}
res[i % n] = (s.empty() ? -1 : nums[s.top()]);
s.push(i % n);
}
return res;
}