The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider the index of next g