My issue
hii everyone,
can anyone explain me the logic to solve this question
My code
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int cost = 0;
int data;
vector < int > arr(n, 0);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr.begin(), arr.end());
for(int i = 0; i < n;i++) {
cost+= abs(arr[i] - i);
}
cout<<cost<<endl;
}
int main() {
// your code goes here
int t;
cin >> t;
while (t--) {
solve();
}
}
Problem Link: MEXimize the Array Practice Coding Problem - CodeChef