


ARR[1,3,6] is a Squareful array as 1+3=4 i.e. 2^2 , 3+6=9 i.e. 3^2.
[1,6,3] and [6,1,3] are different permutations.
The first line contains an integer ‘N’, representing the array’s size.
The second contains 'N' space-separated integers, denoting the elements of the array.
For each test case, return an integer denoting the number of Squareful Permutation of array ARR.
You don’t need to print anything or take input; it already has been taken care of. Just implement the function.
1 <= N <= 12
0 <= ARR[i] <= 10^4 , where 0 <= i < N
Time limit: 1 sec
In this approach, we will check all the permutations of the array. And for each permutation in array ‘ARR’, check if ARR[i]+ARR[i+1] is a perfect square. If it is a perfect square, then increase the answer by 1. Return answer.
Algorithm:-
Make a graph where an edge exit from i to j if ARR[i]+ARR[j] is a perfect square. After that count all possible Hamiltonian paths. Total hamiltonian paths will be the number of permutations that are squareful.
Hamiltonian path is a graph path between two vertices of a graph such that all visit all the vertices of the graph at least once.
We will construct the graph where an edge between ‘i’ and ‘j’ if ARR[i] + ARR[j] is a perfect square. We can use dynamic programming to check the hamiltonian paths.
Algorithm:-