Ctrl K

Neighbours With an Even Expression

Medium
Consider arrays arr of length length whose elements are integers from 1 to top. Call a neighbouring pair (positions i and i + 1) marked if arr[i] · arr[i + 1] − arr[i] − arr[i + 1] is even. Return how many arrays have exactly links marked pairs. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
length=2, top=3, links=1 → 1

Among pairs of numbers from 1 to 3, only [2, 2] works: 2 · 2 − 2 − 2 = 0, which is even.

Example 2
length=3, top=2, links=0 → 5

5 of the 8 arrays work: all except [2, 2, 1], [1, 2, 2] and [2, 2, 2].

Example 3
length=1, top=6, links=0 → 6

A one-element array has no neighbour pairs, so any of the 6 numbers works.

Constraints
  • 1 ≤ length ≤ 750
  • 0 ≤ links ≤ length − 1
  • 1 ≤ top ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumAll Balanced Bracket Strings MediumFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell Medium
loading Python…
Input
length=2, top=3, links=1
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 58 tests on the server, hidden ones included.