The maximum range

3.5

6 votes
Sliding Window, Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

You are given an array of size N and an integer K. Your task is to find the largest subarray of the provided array such that the absolute difference between any two elements in the subarray is less than or equal to K.

Let M and m be the maximum and minimum value in the selected largest subarray. Now, Mmk must hold.

Print the length of the largest subarray.

Input format

  • The first line contains two integers N and K.
  • The next line contains K integers.

Output format

Print the answer as described in the problem statement.

Constraints

1N106

1K106

1ArrayElements106

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We can select the subarray starting from index 2 to 4 of length 3.

Editor Image

?