I have this problem: consider all the square numbers with exactly n digits, I want to arrange them such that the last digit of a square is equal to first digit of the next square and find the longest arrangement, how many elements contain and possibly how many of those longest arrangement are possible. Of course some squares will be sorted out, eg.
For n=2 the squares are {16, 25, 36, 49, 64, 81}.
So the longest arrangement is {81, 16, 64, 49}
with s=4 elements
For n=3 there are s=12 elements and one of the possible arrangements is
{841, 121, 144, 484, 441, 169, 961, 196, 676, 625, 529, 900}
Of course there are some criteria (at most 1 number can start with {2,3,7,8} and if so, must be in the 1st position; at most 1 with 0 as last digit)
I have found something similar here https://www.geeksforgeeks.org/arrange-array-elements-such-that-last-digit-of-an-element-is-equal-to-first-digit-of-the-next-element/ but if I insert multiple numbers with same first and last digit it gives me an error.
Maybe would be easier to construct a number like this: naming the squares with n digits {a₁, a₂, a₃,..., aₖ} (values of k for each n https://oeis.org/A049415)
P = aₕ+aᵢ10ⁿ+aⱼ10²ⁿ+...+ aₘ*10⁽ˢ⁻¹⁾ⁿ
h, I, j,...,m < k
and find the biggest possible number P, but I don't know how to set it up
11
, b lots of12
, c lots of21
, d lots of22
. With these a+b+c+d numbers, by gluing some of them linearly, you are planning to make longest number. I think expressing the answer of this simplified problem in a,b,c,d can be a starting point. $\endgroup$