Next Greater Element Algorithm