Operations on an Array
Practice
3.9 (13 votes)
Algorithms
Arrays
Brute Force search
Easy
Greedy algorithms
Problem
87% Success 5311 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\)
Explanation
We can decrease the \(3^{rd}\) element by \(1\) and all elements become same. Hence, answer is \(1\).
Code Editor
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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
1.Swap Sum
Points:20
23 votes
Tags:
Basics of Greedy AlgorithmsAlgorithmsGreedy Algorithms
Points:20
22 votes
Tags:
AlgorithmsGreedy Algorithms
Points:20
250 votes
Tags:
ApprovedEasyGreedy AlgorithmsImplementationReady
Editorial
Login to unlock the editorial
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
Results
Custom Input
Run your code to see the output