Company: Ion Group - Hackerrank
Difficulty: medium
A group of workers with different efficiency ratings needs to be paired up to complete a task. When two workers are paired, the cost of that pair is the absolute difference between their efficiency ratings. The total cost of the task is the sum of all pair costs. Since there is an odd number of workers, exactly one worker will be left unpaired. Your goal is to select which worker to exclude so that the total cost is minimised, and report that minimum cost. Input Format The first line contains an integer n , the number of workers. Each of the next n lines contains one integer efficiency[i] . Output Format Print a single integer — the minimum possible total cost. Constraints 3 ≤ n < 10 5 1 ≤ efficiency[i] ≤ 10 9 n is odd Sample Input 0 5 4 1 2 16 8 Sample Output 0 5 Explanation. Exclude worker 3 (rating 16) and form the pairs (1, 2) and (4, 8). The cost is |1 − 2| + |4 − 8| = 5 . Sample Input 1 7 2 13 12 9 6 3 2 Sample Output 1 4 Explanation. Exclude th