Swap two numbers among first n positive integers such that first m numbers have equal sum as the last m-n numbers The problem provides a natural number $n$. We have to consider the tuple $(1,2,\dots,n)$. Now, any $2$ numbers in that tuple can be swapped if and only if $\exists m, 1\leq m<n$, such that the prefix sum (sum of all elements $a_i,i\le m$) would be equal to the suffix sum (sum of all elements $a_i,i\gt m$) after the swap.
How many possible swaps are there, for a given $n$?
Let me elaborate this with an example. Let $n = 7$, which gives the tuple $(1, 2, 3, 4, 5, 6, 7)$.
In this tuple, $1$ and $5$ can be swapped because for $m = 4$, prefix sum $= 5+2+3+4 = 14$ and suffix sum $= 1+6+7 = 14$ are the same.
Similarly $2$ and $6$ can be swapped, because for $m = 4$, prefix sum $= 1+6+3+4 = 14$ and suffix sum $= 5+2+7 = 14$ are the same.
Same logic applies for the swapping of $3$ and $7$. So far, we found at least three swaps for $n=7$. How many are there in total?
I need to calculate the number of such possible swaps for large $n$ values. There must be some sort of formula. I have tried a number of ways to approach this but failed.
 A: The prefix sum of $m$ is $\frac{m(m-1)}{2}$ and the suffix sum is $\frac{n(n+1)}{2}-\frac{m(m+1)}{2}$. A successful swap of $a<m$ with $b=a+\delta>m$ would turn these into
$\frac{m(m-1)}{2}+\delta$ and $\frac{n(n+1)}{2}-\frac{m(m+1)}{2}-\delta$, respectively.
We want to achieve that these are equal, i.e.,
$$m^2+2\delta = \frac{n(n+1)}{2}. $$
Clearly, we can make $\delta$ any value in the range $2,\ldots, n-1$ (as long as $1<m<n$).
So, given $n$, compute $$m_0=\left\lfloor\sqrt{\frac{n(n+1)}2-2}\right\rfloor$$
and let $m_1=m_0$ or $m_1=m_0-1$, whichever has the same parity as $\frac{n(n+1)}{2}$.
Finally, let $$\delta = \frac{\frac{n(n+1)}{2}-m_1^2}2.$$
If $\delta<n$ (and $1<m_1<n$, which will but fail only for tiny values of $n$), we succeed by picking $m=m_1$ and $1\le a<m<b\le n$ with $b-a=\delta$; if on the other hand $\delta\ge n$, then no choice of $m,a,b$ is possible.
For $m=m_1$ and $\delta$ as just found, it is also not hard to compute the number of suitable pairs $(a,b)$ as we must have $m+1-\delta\le a\le\min\{m-1,n-\delta\}$; so the count is
$$ \min\{m-1,n-\delta\}-(m+1-\delta)+1 = \min\{\delta-1,n-m\}.$$
A priori, we may need to try again with $m=m_1-2$ etc., but as $m_1\approx\frac n{\sqrt 2}$, this increases $\delta$ by $\approx 2m\approx n\sqrt 2\gg n$, so (except possibly for tiny $n$) no additional values of  $m$ need to be considered.
