Let's say for some $n$ we have the sequence $1, 2, 3, \dots , n$, what we want with this sequence is to divide it in two sets such that each element of the sequence will be in only one set, and the difference in sums of the two sets will be minimum possible.
Example
Let $n = 4$, and $A$ and $B$ are our sets, we can divide the numbers in this set such that the difference in sum between them will be $0$, $A = \{1, 4\}$ and $B = \{2,3\}$
My thinking
I noticed we can divide like this $1$ and $n$ in set $A$, then $2$ and $n-1$ in set $B$, then $3$ and $n-2$ in $A$, etc..
My algorithm gives correct result if $n$ is divisible $4$, but when it is not divisible by $4$ it will return that the difference between the sets is bigger than it actually is, how can we divide the numbers optimally.