FastPrepCount Permutations Without Three Equal Parities in a Row

Count Permutations Without Three Equal Parities in a Row

Google logoGoogle● HardINTERNOA
Learn

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[]) → int

Examples

Example 1

nums = [1,2]return = 2

Both permutations are valid.

Example 2

nums = [1,3,5,2]return = 12

The 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 = 0

Every permutation has three consecutive odd values.

Constraints

  • 1 <= nums.length <= 500.
  • -1000000000 <= nums[i] <= 1000000000.
  • All values in nums are distinct.

More Google problems

See Google hiring insights
public int countParitySafePermutations(int[] nums) {
    // Write your code here.
}
nums[1,2]
expected2
Checking account…