Problem · Array

Maximum of Subarray Minimums

Learn this problem
MediumJPMorgan Chase logoJPMorgan ChaseINTERNOA

Problem 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 array
  • int 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) → int

Examples

Example 1

arr = [1,2,3,4,5]k = 2return = 4
  • The length-2 subarrays are [1,2], [2,3], [3,4], and [4,5].
  • Their minimum values are 1, 2, 3, and 4.
  • The maximum of these values is 4.

Constraints

  • 1 <= n <= 10^6
  • 1 <= arr[i] <= 10^9 for 0 <= i < n
  • 1 <= k <= n

More JPMorgan Chase problems

drafts saved locally
public int maxOfSubarrayMinimums(int[] arr, int k) {
  // Write your code here.
}
arr[1,2,3,4,5]
k2
expected4
checking account