#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], n;
long long solve(int l, int r) {
  if (r - l == 1) return 0;
  int mid = l + r >> 1;
  long long ret = solve(l, mid) + solve(mid, r);
  {
    set<int> s;
    for (int i = mid - 1, j = mid, p = 0; i >= l; i--) {
      p = max(p, a[i]);
      while (j < r && a[j] < p) s.insert(a[j++]);
      ret += s.count(p - a[i]);
    }
  }
  {
    set<int> s;
    for (int i = mid, j = mid - 1, p = 0; i < r; i++) {
      p = max(p, a[i]);
      while (j >= l && a[j] < p) s.insert(a[j--]);
      ret += s.count(p - a[i]);
    }
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  printf("%lld\n", solve(0, n));
  return 0;
}
