<\/span><\/h2>\n\n\n\nIn this Leetcode Next Greater Element I problem solution<\/strong>,<\/p>\n\n\n\nThe next greater element<\/strong> of some element x<\/code> in an array is the first greater<\/strong> element that is to the right<\/strong> of x<\/code> in the same array.<\/p>\n\n\n\nYou are given two distinct 0-indexed<\/strong> integer arrays nums1<\/code> and nums2<\/code>, where nums1<\/code> is a subset of nums2<\/code>.<\/p>\n\n\n\nFor each 0 <= i < nums1.length<\/code>, find the index j<\/code> such that nums1[i] == nums2[j]<\/code> and determine the next greater element<\/strong> of nums2[j]<\/code> in nums2<\/code>. If there is no next greater element, then the answer for this query is -1<\/code>.<\/p>\n\n\n\nReturn an array ans<\/code> of length nums1.length<\/code> such that ans[i] <\/code>is the next greater element<\/strong> as described above.<\/em><\/p>\n\n\n\n<\/span>Example 1 :<\/strong><\/span><\/h3>\n\n\n\n\nInput: nums1 = [4,1,2], nums2 = [1,3,4,2]\r\nOutput: [-1,3,-1]\r\nExplanation: The next greater element for each value of nums1 is as follows:\r\n- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.\r\n- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.\r\n- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.\n<\/code><\/pre>\n\n\n\n<\/span>Example 2 :<\/strong><\/span><\/h3>\n\n\n\n\nInput: nums1 = [2,4], nums2 = [1,2,3,4]\r\nOutput: [3,-1]\r\nExplanation: The next greater element for each value of nums1 is as follows:\r\n- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.\r\n- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.\n<\/code><\/pre>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h3>\n\n\n\n\n1 <= nums1.length <= nums2.length <= 1000<\/code><\/li>\n\n\n\n0 <= nums1[i], nums2[i] <= 104<\/sup><\/code><\/li>\n\n\n\n- All integers in
nums1<\/code> and nums2<\/code> are unique<\/strong>.<\/li>\n\n\n\n- All the integers of
nums1<\/code> also appear in nums2<\/code>.<\/li>\n<\/ul>\n\n\n\nNow, let’s see the code of 496. Next Greater Element I<\/strong><\/strong> – Leetcode Solution.<\/p>\n\n\n\n<\/span>Next Greater Element I<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong> – Leetcode Solution<\/span><\/h1>\n\n\n\n<\/span>496. Next Greater Element I<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong> – Solution in Java<\/strong><\/span><\/h3>\n\n\n\nclass Solution {\n public int[] nextGreaterElement(int[] nums1, int[] nums2) {\n int[] ans = new int[nums1.length];\n for(int i=0;i<nums1.length;i++){\n ans[i] = nextGreaterElement(nums2,nums1[i]);\n }\n return ans;\n }\n\n public int nextGreaterElement(int[] nums, int tar){\n Stack<Integer> s = new Stack<>(); \n for(int e : nums){\n s.add(e);\n }\n int ans = -1;\n while(!(s.isEmpty())){\n int top = s.peek();\n if(top == tar) return ans;\n s.pop();\n \n if(top > tar){\n ans = top;\n } \n }\n return ans;\n }\n}<\/pre>\n\n\n\n<\/span>