-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChange_It Problem Statement
40 lines (34 loc) · 1.7 KB
/
Change_It Problem Statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
You are given a sequence A1,A2,…,AN. You want all the elements of the sequence to be equal. In order to achieve that, you may perform zero or more moves. In each move, you must choose an index i (1≤i≤N), then choose j=i−1 or j=i+1 (it is not allowed to choose j=0 or j=N+1) and change the value of Ai to Aj — in other words, you should replace the value of one element of the sequence by one of its adjacent elements.
What is the minimum number of moves you need to make in order to make all the elements of the sequence equal?
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers A1,A2,…,AN.
Output
For each test case, print a single line containing one integer — the minimum required number of moves.
Constraints
1≤T≤100
1≤N≤100
1≤Ai≤100 for each valid i
Example Input
3
5
1 1 1 1 1
4
9 8 1 8
2
1 9
Example Output
0
2
1
Explanation
Example case 1: No moves are needed since all the elements are already equal.
Example case 3: We can perform one move on either A1 or A2.
Author: 3★kmaaszraa
Editorial: https://discuss.codechef.com/problems/CHNGIT
Tags: cakewalk, cook113, greedy, kmaaszraa, kmaaszraa
Date Added: 15-12-2019
Time Limit: 1 secs
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, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS