let’s assume you have array of length n you have to find next greater element of every element but the element you find for next greater element is closest distance compare to other greater element of that element (its a circular array)
let understand with example -:
ex1.
int [] arr = [1,5,3,8,6,9,4]
output - [3,6,4,9,8,-1,5]
ex2.
int [] arr = [1,6,5,4,9,2,3,8]
output -: [2,8,8,5,-1,3,4,9]