Skip to content

Practice Count the Number of Arrays with K Matching Adjacent Elements

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3405
LeetCode Problem

Count the Number of Arrays with K Matching Adjacent Elements

Problem Statement

Count arrays of length n whose elements are integers from 1 through m and in which exactly k adjacent pairs have equal values. Return the count modulo 10^9 + 7.

Source: Count the Number of Arrays with K Matching Adjacent Elements on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 3, m = 2, k = 1
Output
4

# Example 2

Input
n = 4, m = 2, k = 2
Output
6

# Example 3

Input
n = 5, m = 2, k = 0
Output
2

Constraints

1 <= n <= 10^5
1 <= m <= 10^5
0 <= k <= n - 1

Related LeetCode Problems

Practice more problems similar to Count the Number of Arrays with K Matching Adjacent Elements.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.