#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector a(n); for (auto &it : a) { cin >> it; } int cnt = 1; int curr = a[0]; ll sum = 0; for (int i = 1; i < n; i++) { if (abs(a[i] - curr) == 1) cnt++; else { // every subarray is valid add it to the sum sum += 1LL * cnt * (cnt - 1) / 2; cnt = 1; } curr = a[i]; } sum += 1LL * cnt * (cnt - 1) / 2; cout << sum << "\n"; }