Distribute Elements Into Two Arrays I (LeetCode 3069): Solution and Explanation
Split nums into two arrays: the first element seeds arr1, the second seeds arr2, and each later element joins whichever array currently has the larger last element. Return arr1 followed by arr2. For [2,1,3] the answer is [2,3,1].
Intuition
The rule only ever looks at the last element of each array, so there is nothing to optimize. Following it step by step is the whole solution.
Because the values are distinct, the two tails are never equal and every element has exactly one destination.
Once an element is appended it becomes its array's new tail, which is what the next comparison sees.
Approach: Direct simulation
- Start
arr1 = [nums[0]]andarr2 = [nums[1]]. - For each remaining value
xin order, comparearr1[-1]witharr2[-1]. - If the tail of
arr1is larger, appendxtoarr1; otherwise append it toarr2. - Return
arr1 + arr2.
Step-by-step example
nums = [2,1,3]
- 2 seeds arr1 and 1 seeds arr2.
- For 3, the tails are 2 and 1. Since 2 > 1, 3 is appended to arr1, giving arr1 = [2,3].
- Concatenate: [2,3] + [1] = [2,3,1].
Code: Python, Java, C++
1class Solution:
2 def resultArray(self, nums: List[int]) -> List[int]:
3 arr1 = [nums[0]]
4 arr2 = [nums[1]]
5 for x in nums[2:]:
6 if arr1[-1] > arr2[-1]:
7 arr1.append(x)
8 else:
9 arr2.append(x)
10 return arr1 + arr21class Solution {
2 public int[] resultArray(int[] nums) {
3 int n = nums.length;
4 int[] arr1 = new int[n];
5 int[] arr2 = new int[n];
6 int len1 = 1, len2 = 1;
7 arr1[0] = nums[0];
8 arr2[0] = nums[1];
9 for (int i = 2; i < n; i++) {
10 if (arr1[len1 - 1] > arr2[len2 - 1]) {
11 arr1[len1++] = nums[i];
12 } else {
13 arr2[len2++] = nums[i];
14 }
15 }
16 System.arraycopy(arr2, 0, arr1, len1, len2);
17 return arr1;
18 }
19}1class Solution {
2public:
3 vector<int> resultArray(vector<int>& nums) {
4 vector<int> arr1 = {nums[0]};
5 vector<int> arr2 = {nums[1]};
6 for (size_t i = 2; i < nums.size(); i++) {
7 if (arr1.back() > arr2.back()) {
8 arr1.push_back(nums[i]);
9 } else {
10 arr2.push_back(nums[i]);
11 }
12 }
13 arr1.insert(arr1.end(), arr2.begin(), arr2.end());
14 return arr1;
15 }
16};Time and space complexity
O(n)
Each element is compared and appended once.
O(n)
The two arrays together hold all n elements.
Other approaches
Fixed-size buffers (time O(n), space O(n))
In languages without growable lists, allocate two arrays of size n with length counters, then copy the second buffer after the first. Same logic, no reallocation.
Common mistakes
- Comparing the new element with a tail instead of comparing the two tails with each other.
- Forgetting that the problem is 1-indexed in the statement: the first two operations use the first two elements of the 0-indexed array.
- Returning arr2 before arr1. The result is always arr1 first.
Frequently asked questions
What if the tails were equal?
It cannot happen here because all values are distinct. The rule would send the element to arr2, since the check is strictly greater.
What does part II change?
Part II decides by counting how many elements in each array are greater than the new value, which needs a sorted structure or a Fenwick tree to stay fast.
Can I build the result in place?
Not easily, since elements interleave between the two arrays. Two buffers and one copy at the end is the simplest correct approach.