Operations on an Array
Tag(s):

Algorithms, Arrays, Brute-force search, Easy, Greedy algorithm

Problem
Editorial
Analytics

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

• $1 \le N \le 10^3$
• $1 \le A[i] \le 10^9$
SAMPLE INPUT
3
2 2 3
SAMPLE OUTPUT
1
Explanation

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

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...

Contributor Challenge Name

March Easy' 19

OTHER PROBLEMS OF THIS CHALLENGE
• Math > Combinatorics
• Algorithms > Dynamic Programming
• Algorithms > Dynamic Programming
• Data Structures > Advanced Data Structures