Distribute Elements Into Two Arrays II
Updated
Problem statement
You are given an integer array nums of length n. Split its elements into two arrays, arr1 and arr2, by going through nums from left to right:
nums[0]goes intoarr1.nums[1]goes intoarr2.- For each later element
nums[i], count how many elements already inarr1are strictly greater thannums[i], and count the same forarr2. Appendnums[i]to the array with the larger count. - If the two counts are equal, append
nums[i]to the array that has fewer elements. - If the sizes are also equal, append
nums[i]toarr1.
Return the array made of all elements of arr1 followed by all elements of arr2.
Function
resultArray(nums: int[]) → int[]Examples
Example 1
nums = [4,2,5,1]return = [4,5,1,2]Start with arr1 = [4] and arr2 = [2].
- For
5: no element of either array is greater, so the counts are0and0. Both arrays have one element, so5goes toarr1 = [4, 5]. - For
1:arr1has two greater elements (4and5) andarr2has one (2). The larger count wins, soarr1 = [4, 5, 1].
The answer is [4, 5, 1] followed by [2].
Example 2
nums = [5,9,3,9,1]return = [5,3,1,9,9]Start with arr1 = [5] and arr2 = [9].
- For
3: each array has one greater element, and both have one element, soarr1 = [5, 3]. - For
9: no element is greater than9in either array.arr2is shorter, soarr2 = [9, 9]. - For
1: each array has two greater elements, and each has two elements, soarr1 = [5, 3, 1].
The answer is [5, 3, 1] followed by [9, 9].
Constraints
3 <= n <= 20001 <= nums[i] <= 10^9
More Rubrik Online Assessment questions
- Delete and Freeze GameOA · Seen Nov 2026
- Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
- Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
- Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
- Get Minimum OperationsOA · Seen Nov 2024
- Horizontal Pod AutoscalerOA · Seen Nov 2024
- Stock PricesOA · Seen Nov 2024
- StarlightOA · Seen Oct 2024
- Energy CrisisOA · Seen Sep 2024
- Mike and GemsOA · Seen Sep 2024
Similar questions at other companies
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
- Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
- Maximize Shipment Order by Sorting One WindowAmazon · OA
- Incident MonitorStripe · OAStripe interview questions
- Count Pairs with K Divisible Values in the IntervalChubb · OAChubb interview questions
- Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
- Debug the Impact ModelMillennium · OAMillennium interview questions
- Four-Player Trick-Taking Card Game SimulationScale AI · OA · PHONE SCREENScale AI interview questions
- Binary Sorting Rounds After FlipsVisa · OAVisa interview questions