#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 100 * 1000 + 7;
bool rr[N];
int main() {
  int n;
  scanf("%d", &n);
  int arr[n];
  int ord[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    ord[n - arr[i]] = i;
  }
  int resp = 0;
  for (int i = 0; i < n; i++) {
    int l = ord[i];
    int r = ord[i];
    if ((l - 1) < 0 || (r + 1) >= n) continue;
    if (arr[l - 1] > (n - i) || arr[r + 1] > (n - i)) continue;
    while ((r + 1) < n && arr[r + 1] <= (n - i)) {
      r++;
      rr[arr[r]] = true;
    }
    while (l > 0 && arr[l - 1] <= (n - i)) {
      l--;
      if (rr[(n - i) - arr[l]]) resp++;
    }
    memset(rr, false, sizeof rr);
  }
  printf("%d\n", resp);
  return 0;
}
