#3069
Easy Algorithms Distribute elements into two arrays i
Array Simulation
73.8% acceptance
Feb 25, 2026
121
29
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 ith 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.
Return the array result formed by concatenating arr1 and arr2.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn result_array(nums: Vec<i32>) -> Vec<i32> {
let mut arr1 = vec![nums[0]];
let mut arr2 = vec![nums[1]];
for i in 2..nums.len() {
if *arr1.last().unwrap() > *arr2.last().unwrap() { arr1.push(nums[i]); }
else { arr2.push(nums[i]); }
}
arr1.extend(arr2);
arr1
}
}