Find the Number of K-Even Arrays - Problem
You are given three integers n, m, and k. An array arr is called k-even if there are exactly k indices such that, for each of these indices i (0 ≤ i < n - 1):
(arr[i] * arr[i + 1]) - arr[i] - arr[i + 1] is even.
Return the number of possible k-even arrays of size n where all elements are in the range [1, m]. Since the answer may be very large, return it modulo 109 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
n = 3, m = 2, k = 1
›
Output:
4
💡 Note:
Arrays with exactly 1 even pair: [1,1,2], [2,1,1], [1,2,2], [2,2,1]. Each has one adjacent pair with same parity making (a*b-a-b) even.
Example 2 — No Even Pairs
$
Input:
n = 2, m = 2, k = 0
›
Output:
2
💡 Note:
Arrays with 0 even pairs: [1,2], [2,1]. Different parities make (1*2-1-2)=-1 odd, (2*1-2-1)=-1 odd.
Example 3 — All Even Pairs
$
Input:
n = 2, m = 2, k = 1
›
Output:
1
💡 Note:
Arrays with exactly 1 even pair: [2,2]. Only when both adjacent numbers are even does (a*b-a-b) become even: (2*2-2-2)=0.
Constraints
- 1 ≤ n ≤ 1000
- 1 ≤ m ≤ 1000
- 0 ≤ k ≤ n-1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code