Kevin and Sticks
Practice
3.9 (16 votes)
Mathematics
Medium
Greedy algorithm
Problem
43% Success 1993 Attempts 30 Points 7s Time Limit 256MB Memory 1024 KB Max Code
Kevin has n sticks. i-th stick has length \(a_i\).
Kevin wants every stick has the same length. To achieve his goal he can perform one of the following operation in one second:
- Increase length of one stick by 1.
- Reduce the length of one stick. If the stick has length L, the new length will be \(\lfloor {L \over 2} \rfloor\).
Your task is to help Kevin and say the minimum number of seconds to make all sticks have equal length.
Input format:
The first line of input will contain an integer t, denoting the number of test cases.
Each test case starts with the integer n. The next line contains n space-separated integers ai.
Output format:
For every test case output the minimum number of seconds Kevin need to achieve his goal.
Constraints:
- \(1 \le t \le 10\)
- \(1 \le n \le 1000, 1 \le a_i \le 1000\) in 20% of the test cases.
- \(1 \le n \le 1000, 1 \le a_i \le 10^9\) in 40% of the test cases.
- \(1 \le n \le 10^5, 1 \le a_i \le 10^9\) in 40% of the test cases.
Submissions
Please login to view your submissions
Similar Problems
Points:20
30 votes
Tags:
ArraysData StructuresEasyOne-dimensional
Points:20
6 votes
Tags:
SortingImplementationRecruitapprovedEasyApproved
Points:30
Tags:
ReadyMediumAlgorithmsApproved
Editorial