#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int n, ans = 0;
  cin >> n;
  vector<int> a(n), id(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    id[a[i]] = i;
  }
  set<int> s = {-1, n};
  for (int i = n; i >= 1; --i) {
    auto it = s.upper_bound(id[i]);
    int r = (*it) - 1, l = *(--it) + 1;
    s.insert(id[i]);
    if (id[i] - l < r - id[i])
      for (int j = l; j < id[i]; ++j)
        ans += (id[i - a[j]] > id[i]) && (id[i - a[j]] <= r);
    else
      for (int j = id[i] + 1; j <= r; ++j)
        ans += (id[i - a[j]] >= l) && (id[i - a[j]] < id[i]);
  }
  cout << ans;
}
