Next Greater Element I

Easy

Problem Name: Next Greater Element I

Problem Statement: Given two arrays of integers

nums1
and
nums2
, find the
nums2
index of each element in
nums1
.

More formally, given a list of numbers

nums2
and a list of indices
indices
from a separate list of numbers
nums1
, determine the next greater number for each element at the given index in
nums2
.

Rules and Constraints:

  • The function should return a list of indices where elements from
    nums2
    are in
    nums1
    in ascending order of their corresponding indices, and each index is a position within the list
    nums1
    .
  • For each index in
    nums1
    where the element in
    nums2
    is the largest element, the answer will be -1.

Notes: This problem assumes that the input arrays are valid, with

nums1
containing unique integers and
nums2
containing integers without duplicates. The goal is to write an efficient algorithm to solve this problem, focusing on optimizing time complexity.

Input/Output Format: Two arrays,

nums1
and
nums2
, will be provided. The output will be an array of indices, each index pointing to a value in
nums2
corresponding to each element in
nums1
in the ascending order of its index within
nums1
.

Example

Input: {"nums1":[4,1,2],"nums2":[1,3,4,2]} Output: [-1,3,-1]

CompaniesMicrosoft
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.