Watch 10 video solutions for Build Array Where You Can Find The Maximum Exactly K Comparisons, a hard level problem involving Dynamic Programming, Prefix Sum. This walkthrough by codestorywithMIK has 20,499 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:
You should build the array arr which has the following properties:
arr has exactly n integers.1 <= arr[i] <= m where (0 <= i < n).arr, the value search_cost is equal to k.Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7.
Example 1:
Input: n = 2, m = 3, k = 1 Output: 6 Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
Example 2:
Input: n = 5, m = 2, k = 3 Output: 0 Explanation: There are no possible arrays that satisfy the mentioned conditions.
Example 3:
Input: n = 9, m = 1, k = 1 Output: 1 Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
Constraints:
1 <= n <= 501 <= m <= 1000 <= k <= nProblem Overview: You need to build an array of length n with values from 1 to m. While scanning the array from left to right, every time a new element becomes the maximum, the search cost increases by one. The task is to count how many arrays produce exactly k such maximum updates.
Approach 1: Recursion with Memoization (Top-Down DP) (Time: O(n * m * k), Space: O(n * m * k))
This approach models the problem as a recursive state. At position i, you track the current maximum value maxSoFar and how many comparisons (cost) have already been used. From this state, try placing every number from 1 to m. If the chosen value is greater than maxSoFar, the cost increases and the new maximum updates. Otherwise, the cost stays the same. Memoization stores results for the state (i, maxSoFar, cost) so repeated subproblems are avoided. This converts exponential recursion into polynomial dynamic programming. The state space has at most n * m * k combinations.
Approach 2: Dynamic Programming with 3D Table + Prefix Sum Optimization (Time: O(n * m * k), Space: O(n * m * k))
The bottom-up DP solution builds results iteratively. Define dp[i][j][c] as the number of arrays of length i where the maximum value is j and the search cost is c. Two transitions exist when extending the array:
1) Append a value ≤ j, which keeps the maximum unchanged. There are j choices, so multiply by j.
2) Append value j as a new maximum. This means the previous maximum was smaller, so sum all states with maximum < j and cost c-1.
The second transition would normally require iterating over all smaller maxima, but a running prefix sum speeds this up. Instead of scanning repeatedly, maintain cumulative sums across the j dimension. This reduces an extra loop and keeps the overall complexity at O(n * m * k). The method is deterministic, iterative, and typically faster in practice than recursion.
Both approaches rely on classic Dynamic Programming state modeling. The optimized transition uses cumulative sums similar to techniques in Prefix Sum problems. Thinking in terms of "current maximum" and "cost so far" is the key observation that unlocks the DP formulation.
Recommended for interviews: The 3D dynamic programming solution with prefix-sum optimization. Interviewers expect you to define the DP state (index, currentMax, cost) and derive the transitions clearly. Starting with a memoized recursive formulation shows strong problem decomposition skills, while converting it to an iterative DP demonstrates optimization awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursion with Memoization | O(n * m * k) | O(n * m * k) | Good for understanding the state transition and building intuition before converting to iterative DP |
| 3D Dynamic Programming | O(n * m * k * m) | O(n * m * k) | Straightforward implementation but slower due to iterating over smaller maxima |
| 3D DP with Prefix Sum Optimization | O(n * m * k) | O(n * m * k) | Best practical solution when m is large; avoids repeated summation using prefix sums |