I'm a newbie and learning how to use the c++ language in a more flexible way.
In a contest problem I have seen someone write code like this:
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &it : a) cin >> it;
sort(a.begin(), a.end());
bool ok = true;
for (int i = 1; i < n; ++i) {
ok &= (a[i] - a[i - 1] <= 1);
}
if (ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
I'm assuming that this line vector<int> a(n);
declares a vector and allocates space.
My doubts are:
I'm confused on how this for
(auto &it : a) cin >> it;
works.I also don't understand the expression
ok &= (a[i] - a[i - 1] <= 1);
.