Skip to main content

Command Palette

Search for a command to run...

Convert an Array Into a 2D Array With Conditions

Published
2 min read
Convert an Array Into a 2D Array With Conditions
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.

You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:

  • The 2D array should contain only the elements of the array nums.

  • Each row in the 2D array contains distinct integers.

  • The number of rows in the 2D array should be minimal.

Return the resulting array. If there are multiple answers, return any of them.

Note that the 2D array can have a different number of elements on each row.

LeetCode Problem - 2610

class Solution {
    public List<List<Integer>> findMatrix(int[] nums) {
        List<List<Integer>> answer = new ArrayList<>(); // Initialize the list to store the resulting lists of integers

        Map<Integer, Integer> map = new HashMap<>(); // Initialize a map to store the frequency of each integer in nums
        for(int e : nums) { // Loop through each integer in nums
            map.put(e, map.getOrDefault(e, 0) + 1); // Update the frequency count for each integer in the map
        }

        // Initialize the flag to check if all values in the map are zero
        boolean flag = map.values().stream().allMatch(value -> value == 0);

        // Loop until all values in the map are zero
        while(!flag) {
            List<Integer> list = new ArrayList<>(); // Initialize a new list to store the current set of integers
            for(int e : map.keySet()) { // Loop through each key in the map
                int check = map.get(e); // Get the frequency count for the current integer
                if(check > 0) { // If the frequency is greater than zero
                    list.add(e); // Add the integer to the current list
                    map.put(e, map.getOrDefault(e, 0) - 1); // Decrement the frequency count in the map
                }
            }

            answer.add(list); // Add the current list to the answer list
            flag = map.values().stream().allMatch(value -> value == 0); // Update the flag to check if all values are zero
        }

        return answer; // Return the list of lists of integers
    }
}

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.