leetcode

Distribute Elements Into Two Arrays I - Link

Question Description

You are given an integer array nums.

You must split it into two arrays list1 and list2 such that:

Finally, return the concatenation of list1 followed by list2.


Constraints


Approach

Use two ArrayLists to simulate the distribution process with comparison-based placement.

Algorithm:

  1. Initialize list1 with nums[0] and list2 with nums[1]
  2. For each element from index 2 to n-1:
    • Compare last element of list1 with last element of list2
    • If list1.last > list2.last, add to list1
    • Otherwise, add to list2
  3. Create result array and copy elements from list1 followed by list2
  4. Return the concatenated result

This approach works because:


Dry Run

Example 1: nums = [2, 1, 3, 3]

Step-by-step execution:

Result: [2, 3, 3, 1]

Example 2: nums = [5, 4, 3, 8]

Step-by-step execution:

Result: [5, 3, 4, 8]

Example 3: nums = [1, 3]

Step-by-step execution:

Result: [1, 3]


Solution

class Solution {
    public int[] resultArray(int[] nums) {
        int length = nums.length;

        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        list1.add(nums[0]);
        list2.add(nums[1]);

        for (int i = 2; i < length; i++) {
            int lastList1 = list1.get(list1.size() - 1);
            int lastList2 = list2.get(list2.size() - 1);

            if (lastList1 > lastList2) {
                list1.add(nums[i]);
            } else {
                list2.add(nums[i]);
            }
        }

        int[] ans = new int[length];
        int index = 0;
        for (int ele : list1) {
            ans[index++] = ele;
        }
        for (int ele : list2) {
            ans[index++] = ele;
        }
        return ans;
    }
}

Time and Space Complexity


Alternative Approaches

Array-Based Approach (More Memory Efficient)

class Solution {
    public int[] resultArray(int[] nums) {
        int n = nums.length;
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        list1.add(nums[0]);
        list2.add(nums[1]);

        for (int i = 2; i < n; i++) {
            if (list1.get(list1.size() - 1) > list2.get(list2.size() - 1)) {
                list1.add(nums[i]);
            } else {
                list2.add(nums[i]);
            }
        }

        // Build result array
        int[] result = new int[n];
        int idx = 0;
        for (int num : list1) result[idx++] = num;
        for (int num : list2) result[idx++] = num;

        return result;
    }
}

Explanation: Same logic but with more explicit variable naming.

Time Complexity: O(n) - same as main approach Space Complexity: O(n) - same as main approach

Pre-sized ArrayList Approach

class Solution {
    public int[] resultArray(int[] nums) {
        int n = nums.length;
        List<Integer> list1 = new ArrayList<>(n);
        List<Integer> list2 = new ArrayList<>(n);

        list1.add(nums[0]);
        list2.add(nums[1]);

        for (int i = 2; i < n; i++) {
            int last1 = list1.get(list1.size() - 1);
            int last2 = list2.get(list2.size() - 1);

            if (last1 > last2) {
                list1.add(nums[i]);
            } else {
                list2.add(nums[i]);
            }
        }

        int[] result = new int[n];
        for (int i = 0; i < list1.size(); i++) {
            result[i] = list1.get(i);
        }
        for (int i = 0; i < list2.size(); i++) {
            result[list1.size() + i] = list2.get(i);
        }

        return result;
    }
}

Explanation: Pre-size ArrayLists and use indexed assignment for result.

Time Complexity: O(n) - same performance Space Complexity: O(n) - same as main approach

Stream API Approach (Java 8+)

class Solution {
    public int[] resultArray(int[] nums) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        list1.add(nums[0]);
        list2.add(nums[1]);

        for (int i = 2; i < nums.length; i++) {
            int last1 = list1.get(list1.size() - 1);
            int last2 = list2.get(list2.size() - 1);

            if (last1 > last2) {
                list1.add(nums[i]);
            } else {
                list2.add(nums[i]);
            }
        }

        return Stream.concat(list1.stream(), list2.stream())
                    .mapToInt(Integer::intValue)
                    .toArray();
    }
}

Explanation: Use Stream API for concatenation instead of manual copying.

Time Complexity: O(n) - stream operations Space Complexity: O(n) - same as main approach


Key Insights

Distribution Strategy:

Edge Cases:

Performance Characteristics: