We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Dynamic Programming
- Construct the Array
Construct the Array
Construct the Array
Your goal is to find the number of ways to construct an array such that consecutive positions contain different values.
Specifically, we want to construct an array with elements such that each element between and , inclusive. We also want the first and last elements of the array to be and .
Given , and , find the number of ways to construct such an array. Since the answer may be large, only find it modulo .
For example, for , , , there are ways, as shown here:
Complete the function countArray
which takes input , and . Return the number of ways to construct the array such that consecutive elements are distinct.
Constraints
Subtasks
- For of the maximum score, and
Sample Input
, ,
Sample Output
Explanation
Refer to the diagram in the challenge statement.