Write a java program to find next greater element in an array. Given an input array, find the next greater element for every element of an array.The next greater element x is the first greater element on the right side of x in an array.

In this tutorial, I am going to explain two approaches and their time complexities to solve this problem. In the first approach, we’ll see how we can find next greater element using two for loops. And after that we can discuss how we can **find next greater element using stack**.

Also, I have explained how to find next greater element using stack through video tutorial.

**For example**:

Let’s take an array: [4, 2, 6, 8, 1]

4 => 6 (First next greater element of 4 is 6)

2 => 6 (Next greater element of 2 is 6)

6 => 8 (Next greater element of 6 is 8)

8 => null

1 => null

→