Problem · Array
Maximum of Subarray Minimums
Learn this problemProblem statement
You are given an integer array arr of length n and an integer k.
Consider every contiguous subarray of length k:
- The first subarray is
arr[0..k-1]. - The next subarray is
arr[1..k], and so on. - The last subarray ends at index
n-1.
For each such subarray, find its minimum value. Among all these minimum values, determine and return the maximum one.
Implement maxOfSubarrayMinimums with these parameters:
int[] arr: the input arrayint k: the length of every contiguous subarray
Return an int: the maximum among the minimum values of all length-k subarrays.
Function
maxOfSubarrayMinimums(arr: int[], k: int) → intExamples
Example 1
arr = [1,2,3,4,5]k = 2return = 4- The length-
2subarrays are[1,2],[2,3],[3,4], and[4,5]. - Their minimum values are
1,2,3, and4. - The maximum of these values is
4.
Constraints
1 <= n <= 10^61 <= arr[i] <= 10^9for0 <= i < n1 <= k <= n