FastPrepNext Greater Element I

Next Greater Element I

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

You are given two arrays of distinct integers, query and reference, where every query value appears in reference.

For each query value, return the first greater value to its right in reference, or -1 when none exists.

Function

nextGreaterElement(query: int[], reference: int[]) → int[]

Examples

Example 1

query = [4,1,2]reference = [1,3,4,2]return = [-1,3,-1]

Four and two have no greater value to their right; one is followed by three.

Example 2

query = [2,4]reference = [1,2,3,4]return = [3,-1]

Three is the next greater value after two, while four has none.

Constraints

  • 1 <= query.length <= reference.length <= 10^5.
  • All values in each array are distinct.
  • Every value in query appears in reference.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] nextGreaterElement(int[] query, int[] reference) {
    // Write your code here.
}
query[4,1,2]
reference[1,3,4,2]
expected[-1,3,-1]
Checking account…