The maximum range
Sliding window
Algorithms
Basics of greedy algorithms
Greedy algorithms
53.0% success
1,499 attempts
1s time
256MB memory
1024KB max code
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, \(M-m\le k\) 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
\(1\le N\le 10^6\)
\(1\le K\le 10^6\)
\(1\le Array Elements\le 10^6 \)
Examples
Input
5 10 2 30 25 21 15
Output
3
Explanation
We can select the subarray starting from index 2 to 4 of length 3.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Loading Editor...