So here's a question my friend recently gave me and ever since I've been trying to solve it without much success:
There's a number $N$, and out of the set $U = \{1,2,3,\ldots,(N-1)\}$ we have to find a subset $S$ such that the sum
$\sum_{i\in S} i^2 = $ $\color{red}{N^2} ~: ~$(corrected what was assumed to be a typo : user2661923)
For instance if $N = 11$, $U = \{1, 2, 3, 4, 5, 6, 7, 8, 9, 10\}$ has the solutions $S_1 = \{1,2,4,10\}$ and $S_2 = \{2,6,9\}$ because both $S_1$ and $S_2$ have square sums of $11$. Yet there's a caveat that the solution having the largest square and the most number of elements is preferable. In this case, $S_1$ is preferable because the largest element in $S_1$ is $10$ compared to that of $S_2$ which is $9$. Also $S_1$ has more elements than $S_2$ which satisfies our criteria.
I'm struggling to find a general deterministic algorithm that can find such subsets, for example what if $N$ is something like $50$? In that case there are many possible ways to build but subsets of squares that add to $50$ yet also to check these two criteria in conjunction proves to be somewhat of a challenge. I'm not sure if there is even such an algorithm, although I'll be happy to be proved otherwise.
What I've tried is that starting from $N-1$ in the set $U = \{1,2,\ldots,N-1\}$ I can count backward, but how do I choose if some number $i$ should be included in the solution set or not? For it may happen with extremely large sets that the way I would get a perfect sum of squares is dependent upon the specific numbers I choose rather than just make a decision based of the number I am currently standing on.
\begin{align*} \bigg(\sum_{x=1}^{n}x^2\bigg) &=\dfrac{n\big(n+1\big)\big(2n+1\big)}{6} \\ \text{The $(n-1)$ sum minus $n^2$ should be zero}\\ \\ \bigg(\sum_{x=1}^{n-1}x^2\bigg)-(x)^2 &=\dfrac{(n-1)\big(n\big)\big(2n-1\big)}{6} -(n)^2 =0\\ \end{align*}
\begin{align*} (n-1)\big(n\big)\big(2n-1\big) - 6(n)^2 =0\\ n (2 n^2 - 9 n + 1)=0\\ \end{align*}
\begin{align*} n\in\bigg\{0, \dfrac{9 \pm\sqrt{73}}{4} \bigg\}\\ \\ \end{align*}
It appears that $S=U=\{0\}.\space $ There areno other integer solutions.