The maximum range
Practice
3.5 (6 votes)
Sliding window
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
52% Success 1448 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB 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 \)
Submissions
Please login to view your submissions
Similar Problems
Points:20
104 votes
Tags:
EasyGreedy Algorithms
2.Dorsplen
Points:20
15 votes
Tags:
AlgorithmsApprovedEasyGreedy Algorithms
Points:20
348 votes
Tags:
EasyGreedy AlgorithmsOpenString Manipulation
Editorial