ORIGINAL CODING QUESTION
Level the team ratings
One move raises or lowers a single rating by exactly 1. Return the fewest moves needed to make all n ratings equal. The final common value may be any integer.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
First integer n, then n integers. Print one integer.
Constraints
1 ≤ n ≤ 200; -1000 ≤ rating ≤ 1000.
Example 1
Input
3 1 2 3
Output
2
Moving 1 up and 3 down to 2 takes 2 moves.
Example 2
Input
4 1 10 2 9
Output
16
Any common value from 2 to 9 costs 9 + 7 = 16 moves.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
The cost of a common value t is the sum of |rating - t|.
Hint 2
Moving t past a rating changes the cost by how many ratings lie on each side.
Hint 3
Sort the ratings and use the middle one (the median) as t; the sum of distances to it is the answer.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →