Brute force would check every element to the right for each query. That's . Use a monotonic stack instead. Process nums2 from right to left. Keep a stack of elements in decreasing order. For each element, pop smaller elements (they can't be anyone's next greater). The stack top is the next greater element. Store results in a hash map for quick lookup.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/stacks/next-greater-element-i-hint
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████████████████████████████████████████████████████████████████████