// Arup Guha
// 5/11/2016
// Solution to USACO 2016 Silver April Problem: Diamond Collector

import java.util.*;
import java.io.*;

public class diamond {

	public static void main(String[] args) throws Exception {

		// Read in data.
		BufferedReader stdin = new BufferedReader(new FileReader("diamond.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int k = Integer.parseInt(tok.nextToken());
        int[] vals = new int[n];
        for (int i=0; i<n; i++)
            vals[i] = Integer.parseInt(stdin.readLine().trim());

		// Sort the data.
		Arrays.sort(vals);

		int low = 0, high = 0;

		// results[i] stores best streak starting at index i.
		int[] results = new int[n];

		// Sweep through the array with two pointers.
		while (high < n) {

            // Valid interval check if it's the best and extend high.
            if (vals[high] - vals[low] <= k) {
                results[low] = Math.max(results[low], high-low+1);
                high++;
            }

            // Must move low to find new intervals.
            else
                low++;
		}

		// Just in case we need these to be accurate.
		for (int i=low+1; i<n; i++)
            results[i] = n-i;

        // lookup[i] = max(results[i...n-1])
        int[] lookup = new int[n];
        lookup[n-1] = results[n-1];
        for (int i=n-2; i>=0; i--)
            lookup[i] = Math.max(lookup[i+1], results[i]);

		// Store best here.
		int res = 0;

		// Try each possible low bound.
		for (int i=0; i<n; i++) {
            int tmp = results[i];
            if (i+results[i] < n) tmp += lookup[i+results[i]];
            res = Math.max(res, tmp);
		}

		// Write result.
		PrintWriter out = new PrintWriter(new FileWriter("diamond.out"));
		out.println(res);
		out.close();
		stdin.close();
	}
}
