Unify Numbers
Practice
3.5 (543 votes)
Implementation
Sorting
Approved
Easy Medium
Problem
12% Success 7909 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Today Omar has assignment problems from his math professor. Yes, Omar has infinite number of questions that he must solve today. The first problem went something like this.

Given N integers in the form of \(A_i\) where \(1 \le i \le N\), Omar wants to make each number \(A_i\) in the N numbers equal to M. To convert a number \(A_i\) to M, it will cost \(|M - A_i|\) units. The problem asks to find out the minimum cost to convert all the N numbers to M, so you should choose the best M to get the minimum cost.

Omar gets bored easily from these kind of questions, can you solve it for him ?

Input:

First line of the input contains an integer T denoting the number of test cases.
First line of every test-case contains an integer N.
Second line of every test case contains N space separated integers \(A_i\).

Output:

For each case, print the minimum cost that Omar needs to pay to convert each \(A_i\) to M, in a separate line.

Constraints:

  • \(1 \le T \le 10\).
  • \(1 \le N \le 10 ^ 5\).
  • \(1 \le A_i \le 10^{9}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
MathematicsMediumOpenApprovedGame Theory
Points:30
103 votes
Tags:
Easy-Medium
Points:20
24 votes
Tags:
Ad-HocBasic ProgrammingEasyMathematicsOpenApprovedMathamatics