This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
-> Last element of array has no rightmost element , so output is -1.
-> Push that element in stack.
-> Traverse the array from right to left (from second last element to leftmost), and check the following.
-> If the current element is smaller than recent top of stack, then that top is next Greater element of that current element, so push the stack top in output vector.
-> Else, check while stack is not empty and stack top is less than the current value, Pop the items from stack.
-> If stack gets empty, that means no NGE of current element exists, so push -1 in output vector.
-> If we found a NGE, then push that top of stack into output vector.
-> Finally, push the current element of array in stack.
-> Since the output vector has elements from right to left order, so reverse the vector and return it.
Screenshots(Attach 2 screenshots of your own input and output) -
Eg - If your code follow the below guidelines. Kindly change [] to [x]
All the conditions should be fulfilled for considering your code for merging -
[x] I have mentioned the question as comment in my solution file.
[x] My code follows the guidelines of this project.
[x] I have performed a self-review of my own code.
[x] I have commented my code.
[x] My code gives the correct output.
[x] I confirm that I have not copied the code from anywhere. In case its found that I have copied even after successful merge then I can be banned from the repository and hacktoberfest.
[x] I affirm that I strictly follow contributing guidelines and code of conduct.
Issue Id you have worked upon - #346
Briefly explain your program logic -
-> Last element of array has no rightmost element , so output is -1.
-> Push that element in stack.
-> Traverse the array from right to left (from second last element to leftmost), and check the following.
-> If the current element is smaller than recent top of stack, then that top is next Greater element of that current element, so push the stack top in output vector.
-> Else, check while stack is not empty and stack top is less than the current value, Pop the items from stack.
-> If stack gets empty, that means no NGE of current element exists, so push -1 in output vector.
-> If we found a NGE, then push that top of stack into output vector.
-> Finally, push the current element of array in stack.
-> Since the output vector has elements from right to left order, so reverse the vector and return it.
Screenshots(Attach 2 screenshots of your own input and output) -
Problem Link - https://practice.geeksforgeeks.org/problems/next-larger-element-1587115620/1
Checklist:
Eg - If your code follow the below guidelines. Kindly change [] to [x]
All the conditions should be fulfilled for considering your code for merging -
[x] I have mentioned the question as comment in my solution file.
[x] My code follows the guidelines of this project.
[x] I have performed a self-review of my own code.
[x] I have commented my code.
[x] My code gives the correct output.
[x] I confirm that I have not copied the code from anywhere. In case its found that I have copied even after successful merge then I can be banned from the repository and hacktoberfest.
[x] I affirm that I strictly follow contributing guidelines and code of conduct.