n, num = map(int, input().split())
nums = list(map(int, input().split()))
import collections
q_max = collections.deque()
q_min = collections.deque()
i, j, res = 0, 0, 0
while i < n:
while j < n:
while not q_min or q_min[-1] != j:
while q_max and nums[q_max[-1]] < nums[j]:
q_max.pop()
q_max.append(j)
while q_min and nums[q_min[-1]] > nums[j]:
q_min.pop()
q_min.append(j)
if nums[q_max[0]] - nums[q_min[0]] > num:
break
j += 1
res += j - i
if q_max[0] == i:
q_max.popleft()
if q_min[0] == i:
q_min.popleft()
i += 1
print(res)