Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 2, 2015 09:27
Show Gist options
  • Save rohith2506/c4d957634aace19920de to your computer and use it in GitHub Desktop.
Save rohith2506/c4d957634aace19920de to your computer and use it in GitHub Desktop.
NGE of an array
// 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