The maximum range
Practice
3.5 (6 votes)
Sliding window
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
52% Success 1446 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
19 votes
Tags:
AlgorithmsApprovedEasyGreedy AlgorithmsOpen
Points:20
16 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:20
1 votes
Tags:
Basics of Greedy AlgorithmsGreedy AlgorithmsAlgorithmsSorting
Editorial