Katkat

Find next greater element for subset

TemuSoftware Engineer · Onsite

Last 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 nums are distinct.
  • Every element in query also appears in nums.

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]