Distribute Elements Into Two Arrays I

You are given an array `nums` of distinct integers. Put `nums[0]` into `arr1` and `nums[1]` into `arr2`. Then, for each later element in order, append it to `arr1` if the last element of `arr1` is greater than the last element of `arr2`, otherwise append it to `arr2`. Return `arr1` followed by `arr2`.

30:00

Distribute Elements Into Two Arrays I
easy
Free written solution
Topics
Companies

You are given an array nums of distinct integers. Put nums[0] into arr1 and nums[1] into arr2. Then, for each later element in order, append it to arr1 if the last element of arr1 is greater than the last element of arr2, otherwise append it to arr2. Return arr1 followed by arr2.

Example 1:
Input: {"nums":[2,1,3]}
Output: [2,3,1]
Constraints:
  • 3≤n≤503 \leq n \leq 50

  • 1≤nums[i]≤1001 \leq nums[i] \leq 100

  • All elements in nums are distinct.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[2,1,3]}

nums (read left to right)

2
1
3
x = 2

seeds arr1

arr1

tail 2

2

arr2

tail -

The first element 2 always starts arr1.

Variables
VariableValue
i0
x2
arr1[-1]2
arr2[-1]null
DepthFunction Call
Stack empty
0/4