- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

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]|

Let us see the following implementation to get better understanding −

## Example

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]))

## Input

[2, 10, 8, 5, 4, 7]

## Output

2

- Related Questions & Answers
- Python program to find sum of absolute difference between all pairs in a list
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- C program to find the median of a given list.
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- Python - Calculate the median of column values of a Pandas DataFrame
- Find smallest values of x and y such that ax – by = 0 in C++
- Program to update list items by their absolute values in Python
- Python program to find the smallest number in a list
- Program to find the sum of the absolute differences of every pair in a sorted list in Python
- How to find the absolute pairwise difference among values of a vector in R?
- Python program to sort tuples by frequency of their absolute difference
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Python program to check if all the values in a list that are greater than a given value

Advertisements