Skip to content

Instantly share code, notes, and snippets.

@fakruboss
Created April 26, 2021 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakruboss/f9479a9f227709d651fbe3d572951e66 to your computer and use it in GitHub Desktop.
Save fakruboss/f9479a9f227709d651fbe3d572951e66 to your computer and use it in GitHub Desktop.
class Solution{
public static long[] nextLargerElement(long[] arr, int n) {
Stack<Integer> stack = new Stack<>();
stack.push(0);
long[] result = new long[n];
Arrays.fill(result, -1);
for (int i = 1; i < n; ++i) {
while (!stack.isEmpty() && arr[i] > arr[stack.peek()]) {
result[stack.pop()] = arr[i];
}
stack.push(i);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment