Dsa
Heap
Medium
K-Peak Distance Finder
Given an array of integers `nums` and an integer `k`, write a program that finds the maximum distance between two indices `i` and `j` (i < j) such that the difference between the maximum and minimum values in the subarray from `i` to `j` is less than or equal to `k`. If no such pair exists, return -1.
Input:
- The first line contains two integers `n` (1 <= n <= 100000) and `k` (0 <= k <= 10^9).
- The second line contains `n` integers, each within the range of -10^9 to 10^9.
Output:
- Output the maximum distance `j - i` for the valid indices or -1 if no such indices exist.
Example:
Input:
5 3
1 5 2 3 4
Output:
3
Key concepts
heappriority-queuesearch
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now