Imagine you have an array of numbers and you want to arrange them in a special way! An array is called squareful if every pair of adjacent elements sum up to a perfect square (like 1, 4, 9, 16, 25, etc.).

Given an integer array nums, your task is to find how many different permutations of this array are squareful. Two permutations are considered different if they differ at any position.

Example: If nums = [1, 17, 8], one valid squareful arrangement is [1, 8, 17] because 1 + 8 = 9 = 3² and 8 + 17 = 25 = 5².

This is a challenging combinatorial problem that combines graph theory, backtracking, and dynamic programming concepts!

Input & Output

example_1.py — Basic Case
$ Input: [1, 17, 8]
Output: 2
💡 Note: The squareful permutations are [1,8,17] and [17,8,1]. For [1,8,17]: 1+8=9=3² and 8+17=25=5². For [17,8,1]: 17+8=25=5² and 8+1=9=3².
example_2.py — Duplicate Numbers
$ Input: [1, 1, 8]
Output: 2
💡 Note: The squareful permutations are [1,8,1] and [1,8,1] - but they're the same! However, since the two 1's are at different positions, we get [1,8,1] and [1,8,1] which count as 2 different permutations.
example_3.py — No Valid Permutation
$ Input: [2, 2, 2]
Output: 0
💡 Note: 2+2=4=2², so adjacent 2's form perfect squares. However, we need all adjacent pairs to be perfect squares. With three 2's, we have two adjacent pairs, both are 2+2=4=2², so actually this should return a positive number for arrangements like [2,2,2].

Visualization

Tap to expand
Squareful Array Visualization18171+8=9=3²8+17=25=5²Valid Path 1: 1 → 8 → 171817✓ Squareful!Valid Path 2: 17 → 8 → 11781✓ Squareful!Algorithm Steps:1. Build graph: Connect numbers whose sum is a perfect square2. Use backtracking to find all Hamiltonian paths in the graph3. Handle duplicates using counting to avoid generating same permutation multiple times
Understanding the Visualization
1
Build Compatibility Graph
Create connections between numbers that sum to perfect squares (1+8=9=3², 8+17=25=5²)
2
Count Number Frequencies
Handle duplicates by counting how many times each unique number appears
3
Find Hamiltonian Paths
Use backtracking to find all paths that use each number exactly as many times as it appears
4
Prune Invalid Branches
Stop exploring paths early when no valid continuation exists
Key Takeaway
🎯 Key Insight: Transform the permutation counting problem into a graph traversal problem! Instead of generating all n! permutations, build a compatibility graph and count Hamiltonian paths using backtracking with smart pruning.

Time & Space Complexity

Time Complexity
⏱️
O(n! × n)

O(n!) to generate all permutations, O(n) to check each one

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for recursion stack and current permutation

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 12
  • 0 ≤ nums[i] ≤ 109
  • Perfect square: A number n is a perfect square if there exists an integer k such that k² = n
Asked in
Google 15 Amazon 8 Meta 12 Microsoft 6
24.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen