#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 1000000007
#define MOD 998244353
#define INF 1000000000000000
vector<ll> v[100001];
ll vis[100001] = {0};
void dfs(ll u) {
  vis[u] = 1;
  for (auto it : v[u]) {
    if (vis[it] == 0) {
      dfs(it);
    }
  }
}
int main() {
  ll n, m, i, k;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll dp[300001] = {0}, j;
  for (i = 0; i < n; i++) {
    ll mini = max(0ll, a[i] - k);
    ll maxi = min(300000ll, a[i] + k);
    ll ans = 0;
    for (j = mini; j <= maxi; j++) {

      ans = max(dp[j] + 1, ans);
    }
    dp[a[i]] = ans;
    //	cout<<dp[a[i]]<<" "<<a[i]<<endl;
  }
  ll ans = 0;
  for (i = 0; i < 300001; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 1000000007
#define MOD 998244353
#define INF 1000000000000000
ll t[3000000] = {0};

void update(ll v, ll tl, ll tr, ll pos, ll new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2 + 1, tl, tm, pos, new_val);
    else
      update(v * 2 + 2, tm + 1, tr, pos, new_val);
    if (t[2 * v + 1] > t[2 * v + 2]) {
      t[v] = t[2 * v + 1];
    } else
      t[v] = t[2 * v + 2];
  }
}
ll sum(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  if ((sum(v * 2 + 1, tl, tm, l, min(r, tm)) >
       sum(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r)))
    return sum(v * 2 + 1, tl, tm, l, min(r, tm));
  else
    return sum(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r);
}
int main() {

  ll n, m, i, k;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll dp[300001] = {0}, j, ans = 0;
  for (i = 0; i < n; i++) {
    ll mini = max(0ll, a[i] - k);
    ll maxi = min(300000ll, a[i] + k);
    ll p = sum(0, 0, 300000, mini, maxi);
    ans = max(ans, p + 1);
    update(0, 0, 300000, a[i], p + 1);
  }

  cout << ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// AtCoder
// #include <atcoder/all>
// using namespace atcoder;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  int sz;
  vector<Monoid> seg;
  const F f;
  const Monoid M1;
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }
  void set(int k, const Monoid &x) { seg[k + sz] = x; }
  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }
  Monoid operator[](const int &k) const { return seg[k + sz]; }
  template <typename C>
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while (a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if (check(nxt))
        a = 2 * a + type;
      else
        M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }
  template <typename C> int find_first(int a, const C &check) {
    Monoid L = M1;
    if (a <= 0) {
      if (check(f(L, seg[1])))
        return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) {
        Monoid nxt = f(L, seg[a]);
        if (check(nxt))
          return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }
  template <typename C> int find_last(int b, const C &check) {
    Monoid R = M1;
    if (b >= sz) {
      if (check(f(seg[1], R)))
        return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for (b += sz; a < b; a >>= 1, b >>= 1) {
      if (b & 1) {
        Monoid nxt = f(seg[--b], R);
        if (check(nxt))
          return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  // SegmentTree<ll> segmin(n, [](ll a, ll b){return min(a, b);}, 10000000);
  // SegmentTree<ll> segmax(n, [](ll a, ll b){return max(a, b);}, 0);
  SegmentTree<int> seg(
      300010, [](int a, int b) { return min(a, b); }, 1000000);
  vector<P> p;
  vector<ll> dp(n + 1, 1);
  reprev(i, n) {
    if (int x = seg.query(max(0, a[i] - k), a[k] + 1); x < n) {
      p.emplace_back(i, x);
    }
    if (int x = seg.query(a[k], min(300010, a[i] + k + 1)); x < n) {
      p.emplace_back(i, x);
    }
    seg.update(a[i], i);
  }
  reverse(all(p));
  for (auto [a, b] : p) {
    eprintf("%d %d\\n", a, b);
    chmax(dp[b], dp[a] + 1);
  }
  ll ans = 0;
  rep(i, n) chmax(ans, dp[i]);
  cout << ans << "\\n";

  /***************************************************/
  /* Use "submit code (with ACL v1)" when using ACL! */
  /***************************************************/
  return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// AtCoder
// #include <atcoder/all>
// using namespace atcoder;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  int sz;
  vector<Monoid> seg;
  const F f;
  const Monoid M1;
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }
  void set(int k, const Monoid &x) { seg[k + sz] = x; }
  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }
  Monoid operator[](const int &k) const { return seg[k + sz]; }
  template <typename C>
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while (a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if (check(nxt))
        a = 2 * a + type;
      else
        M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }
  template <typename C> int find_first(int a, const C &check) {
    Monoid L = M1;
    if (a <= 0) {
      if (check(f(L, seg[1])))
        return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) {
        Monoid nxt = f(L, seg[a]);
        if (check(nxt))
          return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }
  template <typename C> int find_last(int b, const C &check) {
    Monoid R = M1;
    if (b >= sz) {
      if (check(f(seg[1], R)))
        return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for (b += sz; a < b; a >>= 1, b >>= 1) {
      if (b & 1) {
        Monoid nxt = f(seg[--b], R);
        if (check(nxt))
          return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  // SegmentTree<ll> segmin(n, [](ll a, ll b){return min(a, b);}, 10000000);
  // SegmentTree<ll> segmax(n, [](ll a, ll b){return max(a, b);}, 0);
  SegmentTree<int> seg(
      300010, [](int a, int b) { return min(a, b); }, 1000000);
  vector<P> p;
  vector<ll> dp(n + 1, 1);
  reprev(i, n) {
    if (int x = seg.query(max(0, a[i] - k), a[i] + 1); x < n) {
      p.emplace_back(i, x);
    }
    if (int x = seg.query(a[i], min(300010, a[i] + k + 1)); x < n) {
      p.emplace_back(i, x);
    }
    seg.update(a[i], i);
  }
  reverse(all(p));
  for (auto [a, b] : p) {
    eprintf("%d %d\\n", a, b);
    chmax(dp[b], dp[a] + 1);
  }
  ll ans = 0;
  rep(i, n) chmax(ans, dp[i]);
  cout << ans << "\\n";

  /***************************************************/
  /* Use "submit code (with ACL v1)" when using ACL! */
  /***************************************************/
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll N = 3e5, mod = 1e9 + 7, inf = 1e18;
ll seg[3 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\\n";
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll N = 3e5 + 5, mod = 1e9 + 7, inf = 1e18;
ll seg[4 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\\n";
}