Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cpp/Nucleoo_NextGreaterElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
**Problem Statement:** You are given an array consisting of some integer values, you have to find the next greater element for every element, if not found, print -1.


// Function to print next greater element for each array element
void findNextGreater(int arr[], int n) {
stack<int>st;
st.push(arr[0]);
for(int i = 1; i < n; i++){
while(!st.empty() && st.top() < arr[i]) {
cout<<arr[i]<<" ";
st.pop();
}
st.push(arr[i]);
}
while(!st.empty()) {
cout<<"-1"<<" ";
st.pop();
}
}

// Time Complexity : O(n)