{x}
blog image

Build Array Where You Can Find The Maximum Exactly K Comparisons

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).
  • After applying the mentioned algorithm to 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 <= 50
  • 1 <= m <= 100
  • 0 <= k <= n

Solution Explanation:

This problem asks to find the number of arrays of length n with elements in the range [1, m] such that the maximum element is found after exactly k comparisons using the described algorithm. The algorithm involves comparing the current element with the maximum found so far.

The most efficient way to solve this is using dynamic programming. We'll build a 3D DP table:

  • dp[i][c][j] represents the number of ways to construct an array of length i such that the maximum element is j and we've made exactly c comparisons.

Base Case:

  • dp[1][1][j] = 1 for all j from 1 to m. This is because an array of length 1 will always require one comparison and its only element will be the maximum.

Recursive Relation:

To understand the recursive relation, let's consider dp[i][c][j]. To reach a length i array with a maximum of j and c comparisons, there are two possibilities:

  1. The maximum element j is at index i: In this case, the subarray of length i-1 must have had c-1 comparisons, and its maximum element can be any value from 1 to j-1. The number of ways to build this subarray is the sum of dp[i-1][c-1][j0] for all j0 from 1 to j-1. Once we've built the subarray, we add the element j to the end, making a total of c comparisons.

  2. The maximum element j is not at index i: In this case, the maximum element j must have been found in the first i-1 elements. The number of ways to construct such a subarray is dp[i-1][c][j]. We then need to append any element which is less than or equal to j to the array. This appends j ways, and doesn't increase comparison cost (since j is already the maximum)

Combining these, we get the recursive relation:

dp[i][c][j] = (dp[i-1][c][j] * j) % mod + sum(dp[i-1][c-1][j0] for j0 in range(1, j)) % mod

where mod = 10^9 + 7 to handle large numbers.

Final Result:

The final answer is the sum of dp[n][k][j] for all j from 1 to m, since we need the total number of arrays that satisfy the given conditions.

Time and Space Complexity:

  • Time Complexity: O(n * k * m^2). The three nested loops iterate through the dimensions of the DP table. The inner loop to compute the sum adds a factor of m.

  • Space Complexity: O(n * k * m). This is the size of the 3D DP table we use.

Code (Python):

class Solution:
    def numOfArrays(self, n: int, m: int, k: int) -> int:
        mod = 10**9 + 7
        dp = [[[0] * (m + 1) for _ in range(k + 1)] for _ in range(n + 1)]
 
        for j in range(1, m + 1):
            dp[1][1][j] = 1
 
        for i in range(2, n + 1):
            for c in range(1, min(i, k) + 1):
                for j in range(1, m + 1):
                    dp[i][c][j] = (dp[i - 1][c][j] * j) % mod
                    for j0 in range(1, j):
                        dp[i][c][j] = (dp[i][c][j] + dp[i - 1][c - 1][j0]) % mod
 
        ans = 0
        for j in range(1, m + 1):
            ans = (ans + dp[n][k][j]) % mod
 
        return ans
 

The other languages (Java, C++, Go) follow a similar structure, just with different syntax. The core algorithm remains the same.