

import sys

# sys.setrecursionlimit(100000)





def input():

    return sys.stdin.readline().strip()





def input_int():

    return int(eval(input()))





def input_int_list():

    return [int(i) for i in input().split()]





class SegTree:

    X_unit = 0

    X_f = max



    def __init__(self, N):

        self.N = N

        self.X = [self.X_unit] * (N + N)



    def build(self, seq):

        for i, x in enumerate(seq, self.N):

            self.X[i] = x

        for i in range(self.N - 1, 0, -1):

            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])



    def set_val(self, i, x):

        i += self.N

        self.X[i] = x

        while i > 1:

            i >>= 1

            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])



    def fold(self, L, R):

        L += self.N

        R += self.N

        vL = self.X_unit

        vR = self.X_unit

        while L < R:

            if L & 1:

                vL = self.X_f(vL, self.X[L])

                L += 1

            if R & 1:

                R -= 1

                vR = self.X_f(self.X[R], vR)

            L >>= 1

            R >>= 1

        return self.X_f(vL, vR)





def main():

    n, k = input_int_list()

    segtree = SegTree(500000)

    for _ in range(n):

        a = input_int()



        val = segtree.fold(max(0, a - k), min(500000, a + k + 1))

        segtree.set_val(a, val + 1)



    print((segtree.X[1]))



    return





if __name__ == "__main__":

    main()



import sys

# sys.setrecursionlimit(100000)





def input():

    return sys.stdin.readline().strip()





def input_int():

    return int(eval(input()))





def input_int_list():

    return [int(i) for i in input().split()]





class SegTree:

    X_unit = 0

    X_f = max



    def __init__(self, N):

        self.N = N

        self.X = [self.X_unit] * (N + N)



    def build(self, seq):

        for i, x in enumerate(seq, self.N):

            self.X[i] = x

        for i in range(self.N - 1, 0, -1):

            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])



    def set_val(self, i, x):

        i += self.N

        self.X[i] = x

        while i > 1:

            i >>= 1

            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])



    def fold(self, L, R):

        L += self.N

        R += self.N

        vL = self.X_unit

        vR = self.X_unit

        while L < R:

            if L & 1:

                vL = self.X_f(vL, self.X[L])

                L += 1

            if R & 1:

                R -= 1

                vR = self.X_f(self.X[R], vR)

            L >>= 1

            R >>= 1

        return self.X_f(vL, vR)





def main():

    n, k = input_int_list()

    segtree = SegTree(300001)

    for _ in range(n):

        a = input_int()



        val = segtree.fold(max(0, a - k), min(300001, a + k + 1))

        segtree.set_val(a, val + 1)



    print((segtree.X[1]))



    return





if __name__ == "__main__":

    main()

import sys

# sys.setrecursionlimit(100000)
def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(eval(input()))


def input_int_list():
    return [int(i) for i in input().split()]


class SegTree:
    X_unit = 0
    X_f = max

    def __init__(self, N):
        self.N = N
        self.X = [self.X_unit] * (N + N)

    def build(self, seq):
        for i, x in enumerate(seq, self.N):
            self.X[i] = x
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):
        L += self.N
        R += self.N
        vL = self.X_unit
        vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)


def main():
    n, k = input_int_list()
    segtree = SegTree(500000)
    for _ in range(n):
        a = input_int()
        val = segtree.fold(max(0, a - k), min(500000, a + k + 1))
        segtree.set_val(a, val + 1)
    print((segtree.X[1]))
    return


if __name__ == "__main__":
    main()

import sys

# sys.setrecursionlimit(100000)
def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(eval(input()))


def input_int_list():
    return [int(i) for i in input().split()]


class SegTree:
    X_unit = 0
    X_f = max

    def __init__(self, N):
        self.N = N
        self.X = [self.X_unit] * (N + N)

    def build(self, seq):
        for i, x in enumerate(seq, self.N):
            self.X[i] = x
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):
        L += self.N
        R += self.N
        vL = self.X_unit
        vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)


def main():
    n, k = input_int_list()
    segtree = SegTree(300001)
    for _ in range(n):
        a = input_int()
        val = segtree.fold(max(0, a - k), min(300001, a + k + 1))
        segtree.set_val(a, val + 1)
    print((segtree.X[1]))
    return


if __name__ == "__main__":
    main()

-    segtree = SegTree(500000) +    segtree = SegTree(300001) -        val = segtree.fold(max(0, a - k), min(500000, a + k + 1)) +        val = segtree.fold(max(0, a - k), min(300001, a + k + 1))