Count Permutations Without Three Equal Parities in a Row
Problem statement
You are given an array nums of pairwise distinct integers.
Return the number of permutations of all elements in which no three consecutive values are all even and no three consecutive values are all odd. Return the answer modulo 1000000007.
Function
countParitySafePermutations(nums: int[]) → intExamples
Example 1
nums = [1,2]return = 2Both permutations are valid.
Example 2
nums = [1,3,5,2]return = 12The single even value can occupy either middle position. For each valid parity pattern, the three odd values can be arranged in 3! ways.
Example 3
nums = [1,3,5]return = 0Every permutation has three consecutive odd values.
Constraints
1 <= nums.length <= 500.-1000000000 <= nums[i] <= 1000000000.- All values in
numsare distinct.