Problem:
Score : 300 points

Problem Statement

Every day, N passengers arrive at Takahashi Airport.
The i-th passenger arrives at time T_i.

Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers.
Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport.
Also, a passenger will get angry if he/she is still unable to take a bus K units of time after the arrival of the airplane.
For that reason, it is necessary to arrange buses so that the i-th passenger can take a bus departing at time between T_i and T_i + K (inclusive).

When setting the departure times for buses under this condition, find the minimum required number of buses.
Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.

Constraints

2 \leq N \leq 100000

1 \leq C \leq 10^9

1 \leq K \leq 10^9

1 \leq T_i \leq 10^9

C, K and T_i are integers.

Input

The input is given from Standard Input in the following format:

N C K
T_1
T_2
:
T_N

Output

Print the minimum required number of buses.

Sample Input 1

5 3 5
1
2
3
6
12

Sample Output 1

3

For example, the following three buses are enough:

A bus departing at time 4.5, that carries the passengers arriving at time 2 and 3.

A bus departing at time 6, that carries the passengers arriving at time 1 and 6.

A bus departing at time 12, that carries the passenger arriving at time 12.

Sample Input 2

6 3 3
7
6
2
8
10
6

Sample Output 2

3

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

/**
 * Created by ChingYee on 13/3/2017.
 */
import java.util.*;

public class Main {

    public static int minBus(int maxPassenger, int timeLimit, int[] arriveTime) {
        if (arriveTime.length == 0) return 0;
        Arrays.sort(arriveTime);
        int result = 1;
        int curPassenger = 0;
        int busTime = arriveTime[0] + timeLimit;
        // 2 6 6 7 8 10
        for (int i = 0; i < arriveTime.length; i++) {
            if (curPassenger == maxPassenger || arriveTime[i] > busTime) {
                result++;
                busTime = arriveTime[i] + timeLimit;
                curPassenger = 0;
            }
            curPassenger++;
        }
        return result;
    }

    public static int minBus2(int maxPassenger, int timeLimit, int[] arriveTime) {
        int result = 1;
        int curPassenger = 0;
        Arrays.sort(arriveTime);
        int busTime = arriveTime[arriveTime.length-1];
        for (int i = arriveTime.length-1; i >= 0; i--) {
            if (curPassenger == maxPassenger || arriveTime[i]+timeLimit < busTime) {
                result++;
                busTime = arriveTime[i];
                curPassenger = 0;
            }
            curPassenger++;
        }
        return result;
    }



    public static String longestUniqueSub(String s) {
        if (s.length() <= 1) return s;
        int currentStart = 0;
        int resultStart = 0;
        int resultEnd = 0;
        Set<Character> seen = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!seen.contains(c)) {
                seen.add(c);
                if ((i-currentStart) > (resultEnd-resultStart)) {
                    resultStart = currentStart;
                    resultEnd = i;
                }
            } else {
                while (s.charAt(currentStart) != c) {
                    seen.remove(s.charAt(currentStart));
                    currentStart++;
                }
                currentStart++;
            }
        }
        return s.substring(resultStart, resultEnd+1);
    }

    public static boolean isInRange(int currentDifficulty, int nextDifficulty) {
        int difference = nextDifficulty - currentDifficulty;
        if (difference <= 0 || difference > 10) return false;
        return true;
    }

    public static int addProblems(int[] difficulties) {
        int result = 0;
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < difficulties.length; i++) {
            queue.add(difficulties[i]);
        }

        int difficulty = -1;
        int problemsLeft = 4;
        while (!queue.isEmpty()) {
            if (difficulty == -1 || isInRange(difficulty, queue.peek())) {
                difficulty = queue.remove();
                problemsLeft--;
            } else {
                difficulty += 10;
                problemsLeft--;
                result++;
            }
            if (problemsLeft == 0) {
                problemsLeft = 4;
                difficulty = -1;
            }
        }

        if (problemsLeft != 4)
            result += problemsLeft;

        return result;
    }

    public static int addProblems2(int[] difficulties) {
        int result = 0;

        int problemsLeft = 4;
        int difficulty = -1;
        for (int i = 0; i < difficulties.length; i++) {
            int curDifficulty = difficulties[i];
            if (difficulty == -1 || isInRange(difficulty, curDifficulty)) {
                difficulty = curDifficulty;
                problemsLeft--;
            } else {
                difficulty += 10;
                problemsLeft--;
                result++;
                i--;
            }
            if (problemsLeft == 0) {
                problemsLeft = 4;
                difficulty = -1;
            }
        }

        if (problemsLeft != 4)
            result += problemsLeft;

        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int numPassengers = in.nextInt();
        int maxPassenger = in.nextInt();
        int timeLimit = in.nextInt();
        int[] arriveTime = new int[numPassengers];
        for (int i = 0; i < numPassengers; i++) {
            arriveTime[i] = in.nextInt();
        }

        System.out.print(minBus(maxPassenger, timeLimit, arriveTime));
        /*
        System.out.println(longestUniqueSub("ABDEFGABEF"));
        System.out.println(longestUniqueSub("BBBB"));
        System.out.println(longestUniqueSub("GEEKSFORGEEKS"));
        */
        /*
        5
        4
        10 15 25 30
        4
        15 20 25 40
        3
        3 3 3
        8
        60 90 61 62 63 91 92 93
        11
        5 14 30 32 39 46 47 47 30 58 47
                */

        /*
        int[] test1 = new int[] { 10, 15, 25, 30 };

        int[] test2 = new int[] { 15, 20, 25, 40 };

        int[] test3 = new int[] { 3, 3, 3 };

        int[] test4 = new int[] { 60, 90, 61, 62, 63, 91, 92, 93 };

        int[] test5 = new int[] { 5, 14, 30, 32, 39, 46, 47, 47, 30, 58, 47 };

        System.out.println(addProblems2(test1));

        System.out.println(addProblems2(test2));

        System.out.println(addProblems2(test3));

        System.out.println(addProblems2(test4));

        System.out.println(addProblems2(test5));
        */
    }
}