You are given an array of 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 . If the element becomes , it is removed from the array.
- Increase an element by .
You need to find the minimum number of operations required to do so.
Example:
Input: n = 3, a = {2, 2, 3}
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;void operationsOnArray(int n, vector<int> &a){long long ans = LLONG_MAX;for (int i = 0; i < n; ++i){long long here = 0;for (int j = 0; j < n; ++j){here += min(abs(a[i] - a[j]), a[j]);}ans = min(ans, here);}cout << ans << '\n';}int main(){int n = 3;vector<int> a = {2, 2, 3};operationsOnArray(n, a);return 0;}
No comments:
Post a Comment