Skip to content

Commit 0a1f663

Browse files
Create README.md
1 parent 716a76d commit 0a1f663

File tree

1 file changed

+48
-0
lines changed
  • Hard/3405. Count the Number of Arrays with K Matching Adjacent Elements

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Leetcode 3405: Count the Number of Arrays with K Matching Adjacent Elements
2+
3+
## 🚀 Problem Statement
4+
5+
You are given three integers `n`, `m`, and `k`. A *good array* of size `n` is defined as follows:
6+
7+
- Each element is between `1` and `m` (inclusive).
8+
- Exactly `k` indices `i` (where `1 <= i < n`) satisfy `arr[i] == arr[i-1]`.
9+
10+
Your task is to return the number of such good arrays. Since the result may be very large, return the answer modulo `10^9 + 7`.
11+
12+
---
13+
14+
## 🧠 Approach
15+
16+
This is a combinatorics problem that requires:
17+
- Choosing exactly `k` positions out of `n-1` to place equal adjacent elements.
18+
- Assigning values from `1` to `m` such that:
19+
- The first element can be any value from `1` to `m`.
20+
- The `k` chosen positions must match the previous element.
21+
- The other `n - 1 - k` positions must differ from the previous one (i.e., have `m - 1` choices).
22+
23+
### 💡 Final Formula
24+
25+
```python
26+
Total = m × (m - 1)^(n - k - 1) × C(n - 1, k)
27+
```
28+
---
29+
## 🧪 Examples
30+
### Example 1:
31+
### Input:
32+
```python
33+
n = 3, m = 2, k = 1
34+
```
35+
### Output:
36+
```python
37+
4
38+
```
39+
### Explanation:
40+
[1,1,2], [1,2,2], [2,2,1], [2,1,1]
41+
42+
---
43+
## 👨‍💻 Author
44+
Muawiya Amir
45+
AI Student | Full Stack Developer
46+
GitHub: Muawiya-contact
47+
48+
---

0 commit comments

Comments
 (0)