#C5484. Solar Panel Installation Optimization

    ID: 49138 Type: Default 1000ms 256MiB

Solar Panel Installation Optimization

Solar Panel Installation Optimization

You are given N solar panels with efficiencies represented by an array E and an integer K. The task is to select a subset of these panels such that when arranged in non-decreasing order, the difference between the minimum and maximum efficiencies in the selected subset is at most K. In other words, if you select a subset of panels with efficiencies E[i1], E[i2], ..., E[im] (where m ≥ 1), after sorting them, they must satisfy:

$$E_{max} - E_{min} \le K$$

Your goal is to maximize the number of panels in such a subset. This problem can be efficiently solved using a sliding window technique after sorting the array.

inputFormat

The first line contains two integers N and K separated by a space.

The second line contains N integers representing the efficiency values of the solar panels.

For example:

5 3
10 12 14 9 18

outputFormat

Output a single integer denoting the maximum number of solar panels that can be selected such that the difference between the smallest and the largest efficiency is at most K.

For example, the output for the sample input above is:

3
## sample
5 3
10 12 14 9 18
3

</p>