Distribute Integers Between Arrays
Feel free to checkout the image source below for the original problem statement ๐
Once upon a time, there was an array of integers called "numbers," and the task was to divide these numbers between two special arrays, "first" and "second." However, the division wasnโt randomโit followed a set of magical rules. The journey began with the very first number, numbers[0], which was gently placed into the first array. The second number, numbers[1], found its home in the second array. But for every number that followed, starting from numbers[2] and beyond, things became a bit more interesting. Each new number had to find the array where it would be happiest. How was that decided? Well, the number would go to the array that had more elements strictly greater than itself. It was drawn to where the bigger numbers lived! However, if both arrays had the same number of elements greater than the new number, it would choose the array that had fewer elements, preferring to balance things out. And if, after all of that, the arrays were still tied, the number would finally choose the first array as its home. Your quest is to complete the story by combining both arrays into one, with all the numbers from second gracefully appended to the end of first. This combined array will be your final answer. Now go forth and divide the numbers according to these magical rules, returning the grand combination of first and second!
1Example 1
numbers[0] = 5 goes to the first array and numbers[1] = 7 goes to the second array. At this point, first = [5] and second = [7]numbers[2] - 6:
- There are 0 elements in the first array that are greater than
numbers[2] = 6. - There is 1 element in the second array that is greater than
numbers[2], sonumbers[2]goes to the first array. Now,first = [5, 6]andsecond = [7].
numbers[3] = 9, there are no elements greater than 9 in either array, but since both arrays are of equal length, numbers[3] goes to the first array. Now, first = [5, 6, 9] and second = [7].numbers[4] = 2 goes to the first array as well, since there are no elements greater than 2 in either array and the lengths are equal. The final arrays are first = [5, 6, 9, 2] and second = [7].[5, 9, 2, 7, 6].Constraints
Limits and guarantees your solution can rely on.
Unknown for now