Build Array Where You Can Find The Maximum Exactly K Comparisons in C++

C++Server Side ProgrammingProgramming

Suppose we have three integers n, m, and k. If we have following algorithm to find the maximum element of an array of positive integers −

max_val := -1
max_ind := -1
search_cost := 0
n := size of arr
for initialize i := 0, when i < n, update (increase i by 1), do:
   if max_val < arr[i], then:
      max_val := arr[i]
      max_ind := i
      (increase search_cost by 1)
return max_ind

We have to make the array arr which has the following criteria: arr has exactly n integers. all elements arr[i] are in range 1 to m(including) (0 <= i < n). After applying the above algorithm to arr, the value search_cost is equal to k. We have to find the number of ways to build the array arr under the mentioned conditions. The answer may be very large so it will be computed modulo 10^9 + 7.

So, if the input is like n = 2, m = 3, k = 1, then the output will be 6 as the possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]

To solve this, we will follow these steps −

  • m := 10^9 + 7

  • Define a function add(), this will take a, b,

  • return ((a mod m) + (b mod m)) mod m

  • Define an array dp of size: 54 x 54 x 105.

  • Define a function help(), this will take idx, m, k, currVal, n,

  • if k < 0, then −

    • return 0

  • if idx is same as n + 1, then −

    • return true when k is 0

  • if dp[idx, k, currVal + 1] is not equal to -1, then −

    • return dp[idx, k, currVal + 1]

  • ret := 0

  • for initialize i := 1, when i <= m, update (increase i by 1), do −

    • if i > currVal, then −

      • ret := add(help(idx + 1, m, k - 1, max(currVal,i), n), ret)

    • Otherwise

      • ret := add(help(idx + 1, m, k, max(currVal,i), n), ret)

  • return dp[idx, k, currVal + 1] = ret

  • From the main method do the following −

  • for initialize i := 0, when i < 54, update (increase i by 1), do −

    • for initialize j := 0, when j < 54, update (increase j by 1), do −

      • for initialize k := 0, when k < 105, update (increase k by 1), do −

        • dp[i, j, k] := -1

    • ret := help(1, m, k, -1, n)

  • return ret

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
const lli m = 1e9 + 7;
class Solution {
public:
   lli add(lli a, lli b) {
      return ((a % m) + (b % m)) % m;
   }
   int dp[54][54][105];
   int help(int idx, int m, int k, int currVal, int n) {
      if (k < 0)
         return 0;
      if (idx == n + 1) {
         return k == 0;
      }
      if (dp[idx][k][currVal + 1] != -1)
         return dp[idx][k][currVal + 1];
      int ret = 0;
      for (int i = 1; i <= m; i++) {
         if (i > currVal) {
            ret = add(help(idx + 1, m, k - 1, max(currVal, i), n), ret);
         }
         else {
            ret = add(help(idx + 1, m, k, max(currVal, i), n), ret);
         }
      }
      return dp[idx][k][currVal + 1] = ret;
   }
   int numOfArrays(int n, int m, int k) {
      for (int i = 0; i < 54; i++)
         for (int j = 0; j < 54; j++)
            for (int k = 0; k < 105; k++)
               dp[i][j][k] = -1;
      int ret = help(1, m, k, -1, n);
      return ret;
   }
};
main() {
   Solution ob;
   cout << (ob.numOfArrays(2, 3, 1));
}

Input

2, 3, 1

Output

6
raja
Published on 21-Jul-2020 09:17:17
Advertisements