Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, a[N], b[N];
int check(int chn) {
for(int i = 0; i < n; ++i)
b[i] = a[i];
int fac = b[1] - b[0];
for(int i = 2; i < n; ++i) {
if(b[i] - b[i-1] == fac)
continue;
if((b[i] - 1) - b[i-1] == fac) {
--b[i];
++chn;
continue;
}
if((b[i] + 1) - b[i-1] == fac) {
++b[i];
++chn;
continue;
}
return 1e9;
}
return chn;
}
int main() {
cin >> n;
for(int i = 0; i < n; ++i)
cin >> a[i];
if(n <= 2) {
cout << 0 << endl;
return 0;
}
int res = 1e9;
res = check(0); // .. ..
++a[0], ++a[1];res = min(res, check(2));--a[0], --a[1]; // ++ ++
--a[0], --a[1];res = min(res, check(2));++a[0], ++a[1]; // -- --
++a[0];res = min(res, check(1));--a[0]; // ++ ..
++a[1];res = min(res, check(1));--a[1]; // .. ++
--a[0];res = min(res, check(1));++a[0]; // -- ..
--a[1];res = min(res, check(1));++a[1]; // .. --
--a[0], ++a[1];res = min(res, check(2));++a[0], --a[1]; // -- ++
++a[0], --a[1];res = min(res, check(2));--a[0], ++a[1]; // ++ --
if(res == 1e9)
cout << -1 << endl;
else
cout << res << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
4
24 21 14 10
24 21 14 10
Output
3
Demonstration
Codeforces Solution-D. Almost Arithmetic Progression-Solution in C, C++, Java, Python