28. Longest Bounded-Difference Subarray
β GFG solution to the Longest Bounded-Difference Subarray problem: find maximum length subarray where absolute difference between any two elements β€ x using monotonic deque technique. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [8, 4, 5, 6, 7], x = 3
Output: [4, 5, 6, 7]
Explanation: The sub-array described by index [1..4], i.e. [4, 5, 6, 7]
contains no two elements whose absolute difference is greater than 3.
Max difference = 7 - 4 = 3, which is β€ x.Example 2
Input: arr[] = [1, 10, 12, 13, 14], x = 2
Output: [12, 13, 14]
Explanation: The sub-array described by index [2..4], i.e. [12, 13, 14]
contains no two elements whose absolute difference is greater than 2.
Max difference = 14 - 12 = 2, which is β€ x.Example 3
π Constraints
β
My Approach
Sliding Window + Monotonic Deques
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated