struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        let value = peek()
        index += 1
        return value
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

let MOD = 998244353

struct Zn: CustomStringConvertible {
    let n: Int

    init(_ n: Int) {
        self.n = n
    }

    var description: String {
        String(self.n)
    }

    static var zero = Zn(0)
    static var one = Zn(1)

    static func + (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n + rhs.n
        return Zn(n < MOD ? n : n - MOD)
    }

    static func + (lhs: Self, rhs: Int) -> Self {
        return lhs + Zn(rhs)
    }

    static func + (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) + rhs
    }

    static func - (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n - rhs.n
        return Zn(n >= 0 ? n : n + MOD)
    }

    static func - (lhs: Self, rhs: Int) -> Self {
        return lhs - Zn(rhs)
    }

    static func - (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) - rhs
    }

    static func * (lhs: Self, rhs: Self) -> Self {
        Zn((lhs.n * rhs.n) % MOD)
    }

    static func * (lhs: Self, rhs: Int) -> Self {
        return lhs * Zn(rhs)
    }

    static func * (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) * rhs
    }

    static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }

    static func += (lhs: inout Self, rhs: Int) {
        lhs = lhs + Zn(rhs)
    }

    static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }

    static func -= (lhs: inout Self, rhs: Int) {
        lhs = lhs - Zn(rhs)
    }

    static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }

    static func *= (lhs: inout Self, rhs: Int) {
        lhs = lhs * Zn(rhs)
    }

    func pow(_ n: Int) -> Self {
        if n == 0 {
            return Zn(1)
        } else if n % 2 == 1 {
            return self * self.pow(n - 1)
        } else {
            return (self * self).pow(n / 2)
        }
    }

    var inverse: Self {
        self.pow(MOD - 2)
    }
}

struct RMQ {
    var dat: [Int]
    var size: Int

    init(size: Int) {
        self.size = 1
        while self.size < size {
            self.size *= 2
        }
        dat = [Int](repeating: Int.min, count: self.size * 2 - 1)
    }

    mutating func update(index: Int, value: Int) {
        var k = index + size - 1
        dat[k] = value
        while k > 0 {
            k = (k - 1) / 2
            dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2])
        }
    }

    func query(a: Int, b: Int) -> Int {
        func f(l: Int, r: Int, k: Int) -> Int {
            if r <= a || b <= l {
                return Int.min
            }
            if a <= l && r <= b {
                return dat[k]
            }
            let m = (l + r) / 2
            let vl = f(l: l, r: m, k: k * 2 + 1)
            let vr = f(l: m, r: r, k: k * 2 + 2)
            return max(vl, vr)
        }
        return f(l: 0, r: size, k: 0)
    }
}

var scanner = Scanner()
let N = scanner.nextInt()
let K = scanner.nextInt()
let A = (0 ..< N).map{ _ in scanner.nextInt() }

var rmq = RMQ(size: 300001)

for i in 0 ..< N {
    let l = max(0, A[i] - K)
    let r = min(300000, A[i] + K)
    let v = rmq.query(a: l, b: r + 1)
    rmq.update(index: A[i], value: max(1, v + 1))
}

print(rmq.dat[0])


Measured memory_bytes: 18488