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: 2
💡 Note: Arrays with exactly 1 even pair: [1,1], [2,2]. Same parities: (1*1-1-1)=-1 is odd, wait... (1*1-1-1)=-1. Actually (2*2-2-2)=0 is even.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ m ≤ 1000
  • 0 ≤ k ≤ n-1

Visualization

Tap to expand
K-Even Arrays: Count arrays with exactly k same-parity pairsInput: n=3, m=2, k=1[1,1,2] ✓[2,1,1] ✓[1,2,2] ✓[2,2,1] ✓[1,2,1] ✗[2,1,2] ✗Parity CheckSame parity:(a*b - a - b) evenResult: 4Key: Expression is even iff both numbers have same parity (both odd or both even)Answer: 4 arrays have exactly 1 same-parity adjacent pair
Understanding the Visualization
1
Input
n=3, m=2, k=1 (array length 3, values 1-2, need 1 even pair)
2
Process
Check all arrays for pairs with same parity
3
Output
Count arrays with exactly k=1 even pairs
Key Takeaway
🎯 Key Insight: The expression (a×b - a - b) is even if and only if a and b have the same parity
Asked in
Google 15 Microsoft 12 Amazon 8
28.0K Views
Medium Frequency
~35 min Avg. Time
845 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