Dsa
Dynamic Programming
Medium
Subarray Maximum Difference
You are given an array of integers. Your goal is to find the maximum difference between any two elements of a contiguous subarray, and return the maximum of those differences. However, you can only choose elements from subarrays of length 'k'.
Input format:
- The first line contains two integers 'n' and 'k' (1 <= n, k <= 1000), where 'n' is the length of the array and 'k' specifies the maximum subarray length.
- The second line contains 'n' space-separated integers representing the elements of the array.
Output format:
- A single integer representing the maximum difference found from any valid subarray of length 'k'.
Example:
Input:
5 3
1 3 -2 4 5
Output:
7
The maximum difference in the subarray [3, -2, 4] is 7 (4 - (-2)).
Key concepts
dynamic_programmingsubarrayssorting
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