Created
March 2, 2015 09:27
-
-
Save rohith2506/c4d957634aace19920de to your computer and use it in GitHub Desktop.
NGE of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.geeksforgeeks.org/next-greater-element/ | |
// simple stack logic. | |
int nge(int a[]){ | |
vector<int> nge; | |
stack<int> stk; | |
stk.push(a[0]); | |
for(int i=1; i<n; i++){ | |
int next = a[i]; | |
while( !stk.empty()){ | |
if(stk.top() < next){ | |
nge.push_back(next); | |
stk.pop(); | |
} | |
} | |
stk.push(next); | |
} | |
return nge; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment