Dynamic Array Description

You are given an array of n integers where each value is between 1 and m.

Some values are unknown (represented by 0).

You know that the absolute difference between any two adjacent elements in the array is at most 1.

Calculate the number of valid arrays that match this description. Return the answer modulo 109 + 7.

Example 1:

Input : n = 3, m = 5, arr = [2, 0, 2]

Output : 3

Explanation: The middle element is unknown (0). The adjacent difference must be at most 1.

Possible arrays : [2,1,2] ,[2,2,2] ,[2,3,2]

hence Total valid arrays = 3.

Example 2:

Input : n = 3, m = 3, arr = [0,0,0]

Output : 17

Explanation : All values are unknown.

Every element must be between 1 and 3 and adjacent difference must be ≤ 1.

All valid arrays satisfying this rule count to 17.

Now Your Turn!

Pick the correct output for the given input

Input : n = 5, m = 3, arr = [1,0,0,0,1]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 105
  • 1 <= m <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int arrayDescription(int n, int m, vector<int>& arr) {
// Write your code here
return 0;
}
};
Test Case

Input:

Arr
M