Operations on an Array
Practice
3.9 (13 votes)
Algorithms
Arrays
Brute Force search
Easy
Greedy algorithms
Problem
87% Success 5304 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
37 votes
Tags:
MathematicsMedium
Points:20
63 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:20
4 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Editorial