Find next greater element for subset
TemuSoftware Engineer · OnsiteLast updated
This question evaluates understanding of sequence analysis and array-processing competencies, including mapping elements between arrays, index lookup, and designing efficient order-dependent algorithms for the "next greater element" problem.
You are given two integer arrays query and nums.
- All elements in
numsare distinct. - Every element in
queryalso appears innums.
For each value x in query, find the next greater element of x in nums: the first element to the right of x's position in nums that is strictly greater than x. If no such element exists, return -1 for that x.
Return an array ans of the same length as query, where ans[i] is the next greater element for query[i].
Example:
query = [4, 1, 2],nums = [1, 3, 4, 2]→ans = [-1, 3, -1]
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.