Minimize the Equation
All submissions for this problem are available.
Joey is given a problem by his friend Chandler. Since he is not good in maths, he told you to help him solve the problem. Can you help Joey to solve the problem.
The problem is that given an array A with N integers, A, A, A, ... , A[N-1], you need to find an integer x, such that the expression
(A - x)2 + (A - x)2 + ... + (A[N-1] - x)2 is minimum possible.
- The first line contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N denoting the number of integers in the array.
- The second line contains N space-separated intergers denoting the array elements.
For each test case output the number x in a new line.
If there are multiple answers output the maximum one.
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 100000
- 0 ≤ A[i] ≤ 1000000
1 5 6 2 1 9 2
For every possible value of x, x = 4 gives the minimum value (6-4)2 + (2-4)2 + (1-4)2 + (9-4)2 + (2-4)2 = 46.
|Tags||binary-search, encoding, greedy, horsbug98, horsbug98, math|
|Time Limit:||1 - 2 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS|
Fetching successful submissions