Question
Prove that the set $$\mathbb{N} \text{ and } S = {x\in \mathbb{R}|x^{2} \in \mathbb{N}}$$ have the same cardinality.
I drew an arrow diagram, and got this equation:
$$ f: \mathbb{N} \rightarrow S $$ $$ f(x) = \left\{\begin{matrix} \sqrt{\frac{x+1}{2}} & \text{when x is odd}\\ -\sqrt{\frac{x}{2}} & \text{when x is even} \end{matrix}\right. $$ Now, I need to prove the bijection of this piecewise function. I get stuck on that.
What I tried:
I know that in order to prove bijection, I need to prove injection & surjection.
Injection: Let $$ f(x_1) = f(x_2) \Rightarrow x_1 = x_2 $$
Now from here, I get stuck in proving $$x_1 = x_2$$
Surjection:
I know I have to try and find the inverse of the equation and prove that $$ y = f(x) $$, but again, with the piecewise, I am stuck.
Incase the LATEX formatting for the piecewise doesn't work (I spent 10 mins trying to make it work), here is a gif

Some things to observe about your function:
For injectivity, your attempt is just saying the definition of injective, that is a good start, but let's think about what that means. Say we have that $f(n)=f(m)$. Then there are three cases:
$f(n)=f(m)>0$, then we know that $n$ and $m$ are both odd, say $n=2k+1$ and $m=2l+1$, and we know that $f(n)=f(2k+1)=\sqrt{\frac{2k+2}{2}}$ and $f(m)=f(2l+1)=\sqrt{\frac{2l+2}{2}}$, so you should have that since $f(m)=f(n)$ that $\sqrt{\frac{2k+2}{2}}=\sqrt{\frac{2l+2}{2}}$, you can use this to show that $k=l$, and then conclude $n=m$.
If $f(n)=f(m)<0$, you can do a similar analysis, but say $n$ and $m$ are even.
If $f(n)=f(m)=0$, then by the note you know $m=n=0$ (you might also want to prove this)
Now for surjectivity. Say you have some $x\in\mathbb{R}$ such that $x^2=n$ for some $n\in \mathbb{N}$. Again, there will be $3$ cases