Operations on an Array

3.9

13 votes
Algorithms, Arrays, Brute-force search, Easy, Greedy Algorithms
Problem

You are given an array A containing N elements. You need to make the elements of the array equal. You are allowed to do the following operations:

  • Decrease an element by 1. If the element becomes 0, it is removed from the array.
  • Increase an element by 1.

You need to find the minimum number of operations required to do so.

Input Format

  • The First line of input contains an integer N, denoting the number of elements in A.
  • Second line contains N space seperated integers, the elements of the array A.

Output Format

  • Print a single integer representing the number of operations as asked above.

Constraints

  • 1N103
  • 1A[i]109
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We can decrease the 3rd element by 1 and all elements become same. Hence, answer is 1.

Editor Image

?