Number of Squareful Arrays

An array is squareful if the sum of every pair of adjacent elements is a perfect square.

Given an integer array nums, return the number of permutations of nums that are squareful.

Two permutations perm1 and perm2 are different if there is some index i such that perm1[i] != perm2[i].

Example 1
Inputnums = [1,17,8]
Output2
[1,8,17] and [17,8,1] are the valid permutations.
Example 2
Inputnums = [2,2,2]
Output1
All elements are the same, so there is only one distinct squareful permutation.

Constraints

  • 1 <= nums.length <= 12
  • 0 <= nums[i] <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate