1348. Number of Squareful Arrays (Bitmask)

MediumBit ManipulationBitmask DPPermutationCounting

An array is squareful when the sum of every pair of adjacent elements is a perfect square. Given an array, return the number of its distinct permutations that are squareful. Two permutations are the same if they produce identical sequences of values.

Input: A JSON object {"nums": [<non-negative integers>]}.

Output: Return the count of distinct squareful permutations.

Examples

Example 1
Input: {"nums":[1,17,8]}
Output: 2
Explanation: Two arrangements have all adjacent sums square.
Example 2
Input: {"nums":[2,2,2]}
Output: 1
Explanation: All elements equal, giving a single distinct arrangement.

Constraints

Asked by

AppleGoogle
Solve this problem in the editor →