Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to split a list of numbers such that the absolute difference of median values are smallest in Python
Suppose we have a list of numbers called nums, we have to divide it into two parts of same size where the absolute difference between each list's median is as small as possible and we have to find this difference. We have to keep in mind that here length of nums / 2 will be odd.
So, if the input is like [2, 10, 8, 5, 4, 7], then the output will be 2, as we can make two lists like [2,5,10] and [4,7,8], then the medians are 5 and 7, their difference is 2.
Algorithm
To solve this, we will follow these steps ?
- Sort the list nums
- m := quotient of size of nums/2
- return |nums[m] - nums[m-1]|
How It Works
When we sort the array and split it into two equal halves, the optimal way to minimize the median difference is to take the first half as one group and the second half as another. The medians of these groups will be the middle elements, which are nums[m-1] and nums[m] where m = len(nums)//2.
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, nums):
nums.sort()
m = len(nums)//2
return abs(nums[m] - nums[m-1])
ob = Solution()
print(ob.solve([2, 10, 8, 5, 4, 7]))
The output of the above code is ?
2
Step-by-Step Execution
Let's trace through the example [2, 10, 8, 5, 4, 7] ?
nums = [2, 10, 8, 5, 4, 7]
print("Original list:", nums)
# After sorting
nums.sort()
print("Sorted list:", nums)
# Find middle index
m = len(nums)//2
print("Middle index (m):", m)
# Split into two groups
group1 = nums[:m]
group2 = nums[m:]
print("Group 1:", group1)
print("Group 2:", group2)
# Find medians (middle elements since each group has odd length)
median1 = nums[m-1]
median2 = nums[m]
print("Median of group 1:", median1)
print("Median of group 2:", median2)
# Calculate difference
difference = abs(median1 - median2)
print("Minimum difference:", difference)
Original list: [2, 10, 8, 5, 4, 7] Sorted list: [2, 4, 5, 7, 8, 10] Middle index (m): 3 Group 1: [2, 4, 5] Group 2: [7, 8, 10] Median of group 1: 5 Median of group 2: 7 Minimum difference: 2
Alternative Implementation
Here's a more readable version without using a class ?
def min_median_difference(nums):
nums.sort()
m = len(nums) // 2
return abs(nums[m] - nums[m-1])
# Test with different examples
test_cases = [
[2, 10, 8, 5, 4, 7],
[1, 3, 5, 7, 9, 11],
[10, 20, 30, 40]
]
for nums in test_cases:
result = min_median_difference(nums.copy())
print(f"Input: {nums}, Minimum difference: {result}")
Input: [2, 10, 8, 5, 4, 7], Minimum difference: 2 Input: [1, 3, 5, 7, 9, 11], Minimum difference: 2 Input: [10, 20, 30, 40], Minimum difference: 10
Conclusion
The key insight is that after sorting, the optimal split is consecutive elements around the middle. The minimum median difference is always between nums[m-1] and nums[m] where m = len(nums)//2.
