forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// A Stack based C++ program to find next | ||
// greater element for all array elements | ||
// in same order as input. | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
/* prints element and NGE pair for all | ||
elements of arr[] of size n */ | ||
void printNGE(int arr[], int n) | ||
{ | ||
stack<int> s; | ||
unordered_map<int, int> mp; | ||
|
||
/* push the first element to stack */ | ||
s.push(arr[0]); | ||
|
||
|
||
// iterate for rest of the elements | ||
for (int i = 1; i < n; i++) { | ||
|
||
if (s.empty()) { | ||
s.push(arr[i]); | ||
continue; | ||
} | ||
|
||
/* if stack is not empty, then | ||
pop an element from stack. | ||
If the popped element is smaller | ||
than next, then | ||
a) print the pair | ||
b) keep popping while elements are | ||
smaller and stack is not empty */ | ||
while (s.empty() == false && s.top() < arr[i]) { | ||
mp[s.top()] = arr[i]; | ||
s.pop(); | ||
} | ||
|
||
/* push next to stack so that we can find | ||
next smaller for it */ | ||
s.push(arr[i]); | ||
} | ||
|
||
/* After iterating over the loop, the remaining | ||
elements in stack do not have the next smaller | ||
element, so print -1 for them */ | ||
while (s.empty() == false) { | ||
mp[s.top()] = -1; | ||
s.pop(); | ||
} | ||
|
||
for (int i=0; i<n; i++) | ||
cout << arr[i] << " ---> " << mp[arr[i]] << endl; | ||
} | ||
|
||
/* Driver program to test above functions */ | ||
int main() | ||
{ | ||
int arr[] = { 11, 13, 21, 3 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
printNGE(arr, n); | ||
return 0; | ||
} |