PWC 391 Median Boxes
The first task asks us to combine sorted arrays and find the median, and the second asks us to combine boxes by stacking them inside each other. I feel the urge to merge. Task 1: Array Median The median is the middle point in a sorted list. Our musical selection should a couple of songs titled 'The Middle', one by Jimmy Eat World (warning, the video features a lot of people in underwear, probably…
The second task requires merging boxes by stacking them inside one another. To calculate the median of a combined set of sorted arrays, we can follow these steps. First, we need to combine the two input arrays, then sort the merged array. If the total number of elements is odd, the median will be the middle element. If it's even, the median is the average of the two middle elements.
Let's consider the provided examples:
Example 1: Given @arr1 = (2) and @arr2 = (4), the merged array becomes (2, 4). Since there are an even number of elements (2), the median is (2+4)/2 = 3.0.
Example 2: With @arr1 = (1,2,3) and @arr2 = (7,8,9,10), the merged and sorted array is (1,2,3,7,8,9,10). The middle element is the fourth one, which is 7. Hence, the median is 7.0.
Example 3: For @arr1 = () and @arr2 = (10,20,30,40), the merged array is (10,20,30,40). As there are an even number of elements (4), the median is (20+30)/2 = 25.0.
Example 4: Given @arr1 = (100) and @arr2 = (1,2,3,4,5,6,7), the merged and sorted array is (1,2,3,4,5,6,7,100). With an even number of elements (8), the median is (4+5)/2 = 4.5.
Example 5: For @arr1 = (1,2,2) and @arr2 = (2,2,3), the merged array is (1,2,2,2,2,3). The middle elements are the third and fourth, which are both 2. So, the median is (2+2)/2 = 2.0.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.