Next Greater Element I Problem
Next Greater Element I Problem — ExecCode Easy DSA Practice
Solve the Next Greater Element I problem on ExecCode. Free online easy DSA practice in Stack. Write and run code in Java, C++, Python — no signup required to run.
Problem description
The next greater element of x is the first element strictly greater than x that appears to its right in the same array. Core task (monotonic stack): scan nums2 right→left and build nge[i] for every index — one answer per nums2 element. LeetCode 496 twist: nums1 is a subset of nums2; the judge wants only the nge values at nums1's positions (shorter array). The stack still builds the full nums2 nge first.
Examples
Input {"nums1": [2, 1, 5], "nums2": [1, 3, 5, 2, 4]}; Output [4, 3, -1]. Input {"nums1": [4, 1, 2], "nums2": [1, 3, 4, 2]}; Output [-1, 3, -1]. Input {"nums1": [2, 4], "nums2": [1, 2, 3, 4]}; Output [3, -1]
Constraints
1 ≤ nums1.length ≤ nums2.length ≤ 1000 All integers in nums1 and nums2 are unique.
Practice Next Greater Element I free on ExecCode. Browse DSA problems, topic map, and placement guides.