Skip to content

Instantly share code, notes, and snippets.

@SuryaPratapK
Created March 9, 2020 14:01
Show Gist options
  • Save SuryaPratapK/4dc80d52ebe101825c0b0a6e2f4bbcf6 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/4dc80d52ebe101825c0b0a6e2f4bbcf6 to your computer and use it in GitHub Desktop.
// A Stack based C++ program to find next
// greater element for all array elements.
#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;
/* 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])
{
cout << s.top() << " --> " << arr[i] << endl;
s.pop();
}
/* push next to stack so that we can find
next greater for it */
s.push(arr[i]);
}
/* After iterating over the loop, the remaining
elements in stack do not have the next greater
element, so print -1 for them */
while (s.empty() == false) {
cout << s.top() << " --> " << -1 << endl;
s.pop();
}
}
/* 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;
}
@Adi7796
Copy link

Adi7796 commented Jan 8, 2021

Should have written the code that was explained in the video and not copied it from geeks.

@karan68
Copy link

karan68 commented Mar 26, 2021

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
stack mystack;
cin >> n;
int arr[n],ans[n];
for(int i = 0;i < n;++i)
ans[i] = -1;
for(int i = 0;i < n;++i)
cin >> arr[i];
for(int i = 0;i < n;++i)
{
while(!mystack.empty() && arr[i] > arr[mystack.top()])
{
int a = mystack.top();
mystack.pop();
ans[a] = arr[i];
}
mystack.push(i);
}
for(int i = 0;i < n;++i)
cout << arr[i] << " " << ans[i] << endl;
return 0;
}

//my code

@kaisarnajar
Copy link

You are teaching very good sir.

@Prateek14111
Copy link

dont copy code from gfg.

@VineelaKonagala
Copy link

VineelaKonagala commented Mar 6, 2022

`class Solution {

public static int [] nextGreaterElement(int [] a) {
/* write your solution here */
int n=a.length;
int[] b=new int[n];
for(int i=0;i<n;i++)
{
b[i]=-1;
}
Stack st=new Stack();
st.push(0);
for(int i=1;i<n;i++)
{
//int top=st.peek();
while(!st.empty()&&a[st.peek()]<a[i])
{
b[st.peek()]=a[i];
st.pop();
}
st.push(i);
}
return b;
}
}`

@Zaina-fat
Copy link

this solution is too complicated and would give a runtime error on most platforms. Please try simplifying it.

@rohan2734
Copy link

Java code ,
same approach as sir has followed

 public static long[] solveNextLargerElement(long[] arr,int n){
        long[] ans  = new long[n];
        
        Stack<Integer> stack = new Stack<>();
        
        for(int i=0;i< n;i++){
            if(stack.size()==0){
                stack.push(i);
                continue;
            }
            
            while(stack.size()>0 && arr[i] > arr[stack.peek()]){
                ans[stack.pop()]= arr[i];
            }
            
            stack.push(i);
            
           
            
        }
        
        while(stack.size()>0){
            ans[stack.pop()]=-1;
        }
        
        return ans;
    }

@Mritunjay004
Copy link

Java code , same approach as sir has followed

 public static long[] solveNextLargerElement(long[] arr,int n){
        long[] ans  = new long[n];
        
        Stack<Integer> stack = new Stack<>();
        
        for(int i=0;i< n;i++){
            if(stack.size()==0){
                stack.push(i);
                continue;
            }
            
            while(stack.size()>0 && arr[i] > arr[stack.peek()]){
                ans[stack.pop()]= arr[i];
            }
            
            stack.push(i);
            
           
            
        }
        
        while(stack.size()>0){
            ans[stack.pop()]=-1;
        }
        
        return ans;
    }

Thanks 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment