Equal elements

3.8

80 votes
Bit manipulation, Bit Manipulation, Basics of Bit Manipulation, Basic Programming
Problem

You are given an integer array A consisting of N elements. You can perform the following operations on array A:

  • Choose any element and increase or decrease it by 3 for 1 coin.
  • Choose any element and increase or decrease it by 2 for free.

You are required to spend the minimum number of coins in order to make all the elements in array A equal.

Input format

  • 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 elements in array A.
  • The second line of each test case contains N space-separated integers of array A.

Output format

Print T lines. For each test case, print a single line denoting the minimum number of coins to make all elements equal.

Constraints

1T20000

1N100000

1Ai109i[1,N]

Sum of N over all test cases does not exceed 500000

Sample Input
1
4
3 5 2 3
Sample Output
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We can make all elements equal to 3 in 1 coin which is optimal.

  • Increase 2 to 5 for 1 coin.
  • Decrease both  5 to 3 for free
Editor Image

?