pls tell me the problem in the code
Pls tell me the problem in the code
bro pls tell me ur number
i will contact u there
bro i have shared the procedure(in ur previous thread) for sharing the code
pls try it once. it will conveniet for both of us.
if u r not able to save it on cb ide.
then save it over gfg ide-> https://ide.geeksforgeeks.org/
here u have direct option for generating url.
mil gya link bro,mil gya toh yes kr dena
ha mil gya . . . . .
your code is not printing -1.
try this case
4
1 2 3 4
also have u solved next greater element for simple array?
bro pls ccrct my code and send i am now fed up with this problem
pls answer this. . . .
i dont want to brainstorm on this just tell me whether my logic is crrct if yes then what implementation mistake i am doing
otherwise pls tell me the other way of doing the problem
pls refer this->
we use stack stores the indices of the appropriate elements from nums array. The top of the stack refers to the index of the Next Greater Element found so far. We store the indices instead of the elements since there could be duplicates in the nums array. The description of the method will make the above statement clearer.
We start traversing the numsnums array from right towards the left. For an element nums[i] encountered, we pop all the elements stack[top] from the stack such that nums[stack[top]] ≤ nums[i]. We continue the popping till we encounter a stack[top] satisfying nums[stack[top]]>nums[i]. Now, it is obvious that the current stack[top] only can act as the Next Greater Element for nums[i](right now, considering only the elements lying to the right of nums[i]).
If no element remains on the top of the stack, it means no larger element than nums[i] exists to its right. Along with this, we also push the index of the element just encountered(nums[i]), i.e. ii over the top of the stack, so thatnums[i](or stack[topstack[top) now acts as the Next Greater Element for the elements lying to its left.
We go through two such passes over the complete nums array. This is done so as to complete a circular traversal over the nums array. The first pass could make some wrong entries in the res array since it considers only the elements lying to the right of nums[i], without a circular traversal. But, these entries are corrected in the second pass
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;
}
no i have not solved for simple array
and the soln u sent i am able the syntax of language
the above logic also has explanation for the same.
its c++ only…
see it here ->https://ide.codingblocks.com/s/432312
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.