Whys is it -1 for findNums[2] = 2 instead of 4.

• Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

in the above test case why is the result for findNums[2] = -1. Considering the third value is 2 and its corresponding nums value is 4 which is larger than it. So shouldn't the answer be [-1,3,4]

• You have to pick each number from nums1 and have to look at nums2 array:

• For 4, there is no number greater than 4 in nums2(only one number is there and it's 2 which is smaller than 4) so the result is -1

• For 1, 3 is the next greater number in nums2 so the result is 3

• For 2, there is no number after that so the result is -1

I hope it explains well.

• I also don't understand why 4 in nums2 is not greater than 2 in nums1 ie. why 4 is not the next greater number.

• it means that find the elements in nums1 whether there is the next greater number in nums2 or not.
therefore, '2' in nums2 cannot find the next greater number because it is last element in nums2.

• The number 2,in nums1, its place in nums2 is the last, so you cannot find a greater number to its right,thus it returns -1;

• @iamrafiul For 1, 3 is the next greater number in nums2 so the result is 3. Why not 2?

• @Harrywithcode I had the same issue - think of it like you are examining the value of nums1 and the array index of that value in nums2. For example, consider each of the following elements of num1:

4 -> corresponds to index 2 of nums2, so you note that all values beyond that index (i.e. just the value 2) from num2 are not greater. So output[0] = -1

1 -> corresponds to index 0 of nums 2, so you find that index 1 of nums2 with a value of 3 is > 1 (the value from num1).

2 -> corresponds to index 3 of nums2, so there are no elements beyond that value; output[2] = -1

• I was confused with the same problem.
but according to @hstall2529 's answer, which makes sense.
Explain it in a more straightforward way would be like:
Step 1: looking for all the numbers of nums1 in nums2.
for example, nums1=[4,1,2] in nums2 would be nums2 [1,3,4,2].
Step 2: get rid of nums1, find the NEXT GREATER NUMBER ONLY in nums2 [1,3,4,2].
Step 3: And you can see in [1,3,4,2]
The next great number of [1] is 3
The next great number of [4] is null, so -1
The next great number of [2] is null since [2] is the last element, no numbers follow it! So the result is -1
Step 4: Back to nums1's sequence, [ 4,1,2] would return [-1, 3, -1]
Hope this helps!

• @ththlisa
Thanks, it helps me lot!
When I try nums1 = [-1, 1, 2] with nums2 = [1, 3, 4, 2], the expect answer is error, really confused, but with your explaination, it works!

• first you must understand what "The Next Greater Number" mean, it means that you have to find the targer number x in nums2, and then you have to find the first greater number in nums2 from the number x's position to the end position. So you can see the number 2 is the last one data in nums2, there is no other data to the right, so you must push back -1.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.