#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
lint length(lint x)
{
return x * (x + 1) / 2;
}
lint CalculateSubArray(lint arr[], lint n, lint k)
{
lint i = 0;
lint len = 0;
lint result = 0;
lint duplicate_len = 0;
lint max = arr[0];
lint min = arr[0];
lint max_index = 0;
lint min_index = 0;
lint checkpoint_index = 0;
//An adventurer will always progress or get to the same point.
while(i < n)
{
bool MaxChange = max < arr[i];
bool MinChange = min > arr[i];
if(MaxChange) {
max = arr[i];
max_index = i;
}
else if(MinChange) {
min = arr[i];
min_index = i;
}
if(max - min <= k) {
len++;
i++;
duplicate_len += i <= checkpoint_index;
}
else
{
result += length(len);
len = 0;
result -= length(duplicate_len);
duplicate_len = 0;
checkpoint_index = i;
i = (min_index * MaxChange) + (max_index * !MaxChange) + 1;
max = arr[i];
max_index = i;
min = arr[i];
min_index = i;
}
}
result += length(len);
result -= length(duplicate_len);
return result;
}
int main()
{
lint n;
cin >> n;
lint k;
cin >> k;
lint arr[n];
for(lint i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << CalculateSubArray(arr, n, k);
return 0;
}