#include <cstddef>
#include <iostream>
#include <vector>

template <typename T = int>
class SegmentTree {
 private:
  const std::size_t size = 0;
  std::vector<T> tree;

 public:
  explicit SegmentTree(const std::size_t n)
      : size(1 << (32 - __builtin_clz(n))), tree(2 * size, 0) {}

  explicit SegmentTree(const std::vector<T> &vec)
      : size(1 << (32 - __builtin_clz(vec.size()))), tree(2 * size, 0) {
    for (std::size_t i = 0; i < vec.size(); i++) {
      tree[i + size] = vec[i];
    }
    for (std::size_t i = size - 1; i-- > 0;) {
      tree[i] = std::max(tree[2 * i], tree[2 * i + 1]);
    }
  }

  void update(std::size_t idx, const T x) {
    idx += size;
    tree[idx] = std::max(tree[idx], x);
    while (idx > 0) {
      idx /= 2;
      tree[idx] = std::max(tree[2 * idx], tree[2 * idx + 1]);
    }
  }

  T query(std::size_t l, std::size_t r) {
    T x = 0, y = 0;
    for (l += size, r += size; l < r; l /= 2, r /= 2) {
      if (l & 1) x = std::max(x, tree[l++]);
      if (r & 1) y = std::max(tree[--r], y);
    }
    return std::max(x, y);
  }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N, K;
  std::cin >> N >> K;
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    A[i]--;
  }
  SegmentTree st(300000);
  for (int i = 0; i < N; i++) {
    int l = std::max(0, A[i] - K), r = std::min(300000, A[i] + K);
    int x = st.query(l, r + 1);
    st.update(A[i], x + 1);
  }
  std::cout << st.query(0, 300000) << '\n';
  return 0;
}
