Currently taking a test assessment in the Facebook category. The description is as written.
The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
My results match the examples given, but when submitting, the "expected answer" is way off.
My code:
public class Solution {
public int[] NextGreaterElement(int[] nums1, int[] nums2) {
List<int> answer = new List<int>();
foreach(int i in nums1)
{
for(int j = 0; j<nums2.Length; j++)
{
if(nums2[j] == i){
if(j+1 >= nums2.Length || nums2[j+1] <= i)
{
answer.Add(-1);
}
else
{
answer.Add(nums2[j+1]);
}
}
}
}
return answer.ToArray();
}
}INPUT
[1,3,5,2,4]
[6,5,4,3,2,1,7]
OUTPUT
[7,-1,-1,-1,-1]
EXPECTED
[7,7,7,7,7]
The way the proble is described, there is no way this should be an expected answer.
Please resolve this, I had about 15 minutes left in the assessment and now it's ruined.