Equal elements
Practice
3.8 (80 votes)
Bit manipulation
Bit manipulation
Basics of bit manipulation
Basic programming
Problem
92% Success 10531 Attempts 10 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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

\(1 \leq T \leq 20000\)

\(1 \leq N \leq 100000\)

\(1 \leq A_i \leq 10^9 \forall i \in [1,N]\)

Sum of \(N\) over all test cases does not exceed 500000

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:10
8 votes
Tags:
ImplementationBasics of Bit ManipulationBasic ProgrammingBit ManipulationBit manipulation
Points:10
87 votes
Tags:
Basic ProgrammingBit ManipulationC++
Points:10
4 votes
Tags:
Basic MathBasic ProgrammingBasics of bit manipulationBit ManipulationBasics of Bit Manipulation