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
Visualization
Time & Space Complexity
O(n!) to generate all permutations, O(n) to check each one
Space for recursion stack and current permutation
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