Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added C++ solution to 285C Codeforces #441

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Codeforces/285C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*Problem Name (C. Building Permutation)

*Problem Statement :-
You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one.
Count the minimum number of moves, needed to build a permutation from this sequence.

*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){

ll n;cin>>n; // ll is long long data type as defined above
ll arr[n];
for(ll i=0; i<n; i++){
cin>>arr[i];
}

sort(arr,arr+n); //first we sort the array
ll diff =0, sum =0;

for(ll i=0; i<n; i++){

if(arr[i] != i+1){ //now we check if the current element has value equal to (i+1) If no we proceed further

diff = abs(arr[i] - (i+1)); // Now we check the absolute difference between the current element value
// and (i+1) ... (i+1) denotes that there should have been (i+1) instead of arr[i].

sum += diff; // And we add the difference in a variable sum
}
}

cout<<sum<<"\n";


return 0;
}