Count the Number of Arrays with K Matching Adjacent Elements - Problem

You're tasked with counting the number of good arrays of size n where each element is in the range [1, m] and exactly k adjacent pairs have matching values.

What makes an array "good"?

  • The array has exactly n elements
  • Each element arr[i] is between 1 and m (inclusive)
  • There are exactly k indices where arr[i-1] == arr[i] (matching adjacent elements)

Goal: Return the count of all possible good arrays, modulo 10^9 + 7.

Example: For n=3, m=2, k=1, arrays like [1,1,2] and [2,2,1] are good because they have exactly 1 matching adjacent pair.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 3, m = 2, k = 1
โ€บ Output: 6
๐Ÿ’ก Note: With n=3 positions, m=2 colors, and k=1 match needed: Arrays like [1,1,2], [1,2,2], [2,1,1], [2,2,1], [1,2,1] with different arrangements give us 6 valid arrays total.
example_2.py โ€” No Matches
$ Input: n = 2, m = 2, k = 0
โ€บ Output: 2
๐Ÿ’ก Note: Arrays must have no adjacent matches: [1,2] and [2,1] are the only valid arrays.
example_3.py โ€” All Matches
$ Input: n = 3, m = 2, k = 2
โ€บ Output: 2
๐Ÿ’ก Note: Need exactly 2 adjacent matches (all elements same): [1,1,1] and [2,2,2] are the only valid arrays.

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 1 โ‰ค m โ‰ค 1000
  • 0 โ‰ค k โ‰ค n-1
  • Answer fits in 32-bit integer after taking modulo 109 + 7

Visualization

Tap to expand
Building Good Arrays: Step-by-Step ProcessExample: n=4, m=3, k=2 (need exactly 2 adjacent matches)Step 1: Choose first element (3 choices)1Choice 1Step 2: Position 1 options11Match (k=1)12Differ (k=0)Step 3: Continue building...1112 matches usedDynamic Programming State:dp[position][matches_used][last_element_same]State Transitionsโ€ข Match previous: +1 matchโ€ข Differ: choose from m-1โ€ข Memoize resultsKey Insight:๐Ÿ’ก At each position we have 2 choices:1. Match previous element (if matches < k)2. Choose different element (m-1 options)This gives us optimal O(nร—k) complexity!Time Complexity: O(n ร— k ร— 2) = O(n ร— k)Space Complexity: O(n ร— k) for memoization๐Ÿ”ฅ This approach efficiently handles large inputs by avoiding redundant calculations
Understanding the Visualization
1
Choose First Plank
Start with any of the m colors - this gives us m starting possibilities
2
Build Incrementally
At each step, decide: match previous color (if we haven't used all k matches) or pick a different color
3
Track State
Keep track of: current position, matches used so far, and whether we can continue a matching sequence
4
Use Memoization
Cache results for identical subproblems to avoid redundant calculations
Key Takeaway
๐ŸŽฏ Key Insight: Use DP to build arrays incrementally, making optimal choices at each step while tracking matches used
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen