You will be given a list of integers, , and a single integer . You must create an array of length from elements of such that its unfairness is minimized. Call that array . Unfairness of an array is calculated as where: - max denotes the largest integer in - min denotes the smallest integer in as an example, consider the array with a of . Pick any two elements, test . Testing for all pairs, the solution provides the minimum unfairness. Note: integers in may not be unique.
Answers
Answer:
To the right of 6 there is 1 smaller element (1). To the right of 1 there is 0 smaller element. The trivial solution is pretty obvious. For every number in our nums array, we just iterate through the rest of the array and count all of the numbers that are less than that specific number.
Explanation:
Problem:
Given a list of N integers, your task is to select K integers from the list such that its unfairness is minimized.
Unfairness = Max{K integer Set} - Min{K integer Set}
where max denotes the largest integer among the elements of K, and min denotes the smallest integer among the elements of K.
Input Format
- The first line contains an integer N.
- The second line contains an integer K.
- N lines follow. Each line contains an integer that belongs to the list N.
Note : Integers in the list N may not be unique.
Output Format
An integer that denotes the minimum possible value of unfairness.
Constraints
2≤N≤105
2≤K≤N
0≤integer inN≤109