Skip to main content

Command Palette

Search for a command to run...

Next Greater Element I

Published
2 min read
Next Greater Element I
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 496

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        // Initialize a list to store the next greater elements for nums1
        List<Integer> list = new ArrayList<>();

        // Iterate over each element in nums1
        for(int i = 0; i < nums1.length; i++) {
            // Iterate over each element in nums2
            for(int j = 0; j < nums2.length; j++) {
                // Store the current element of nums2
                int nextGreaterElement = nums2[j];

                // Check if the current element of nums1 matches the current element of nums2
                if(nums1[i] == nums2[j]) {
                    // Initialize a flag to track if a greater element is found
                    boolean temp = false;

                    // Iterate over the remaining elements in nums2 starting from the next position
                    for(int k = j + 1; k < nums2.length; k++) {
                        // Check if the current element in nums2 is greater than the matched element
                        if(nums2[k] > nextGreaterElement) {
                            // Add the greater element to the list
                            list.add(nums2[k]);
                            // Set the flag to true indicating a greater element is found
                            temp = true;
                            // Break out of the loop as we found the next greater element
                            break;
                        }
                    }
                    // If no greater element is found, add -1 to the list
                    if (!temp) list.add(-1);
                }
            }
        }

        // Convert the list of Integers to an array of ints
        int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
        // Return the resulting array
        return answer;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.