Skip to main content

Command Palette

Search for a command to run...

Distribute Elements Into Two Arrays I

Published
2 min read
Distribute Elements Into Two Arrays 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.

You are given a 1-indexed array of distinct integers nums of length n.

You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations. In the first operation, append nums[1] to arr1. In the second operation, append nums[2] to arr2. Afterwards, in the i<sup>th</sup> operation:

  • If the last element of arr1 is greater than the last element of arr2, append nums[i] to arr1. Otherwise, append nums[i] to arr2.

The array result is formed by concatenating the arrays arr1 and arr2. For example, if arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].

Return the array result.

LeetCode Problem - 3069

import java.util.ArrayList;

class Solution {
    public int[] resultArray(int[] nums) {
        // Initialize two ArrayLists to store elements based on conditions
        ArrayList<Integer> arr1 = new ArrayList<>();
        ArrayList<Integer> arr2 = new ArrayList<>();

        // Add the first two elements of the input array to respective ArrayLists
        arr1.add(nums[0]);
        arr2.add(nums[1]);

        // Iterate through the rest of the elements of the input array
        for(int i=2; i<nums.length; i++){
            // Compare the last elements of arr1 and arr2
            // Add the current element to arr1 if the last element of arr1 is greater, otherwise add to arr2
            if(arr1.get(arr1.size()-1)>arr2.get((arr2.size()-1))){
                arr1.add(nums[i]);
            } else {
                arr2.add(nums[i]);
            }
        }

        // Initialize a result array with the same length as the input array
        int[] resultArr = new int[nums.length];

        // Copy elements from arr1 to resultArr
        for(int i=0; i<arr1.size(); i++){
            resultArr[i] = arr1.get(i);
        }

        // Add elements from arr2 to resultArr
        int len = arr1.size();
        for (Integer integer : arr2) {
            resultArr[len] = integer;
            len++;
        }

        return resultArr;
    }
}

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.