Let $n\gt1$, and $h(n)$ denote the number of invertible residue classes $mod n$ that are self-inverses.
Show that $h(n)=\phi(n)$ iff $n=2,3,4,6,8,12, or 24$.
Let $n\gt1$, and $h(n)$ denote the number of invertible residue classes $mod n$ that are self-inverses.
Show that $h(n)=\phi(n)$ iff $n=2,3,4,6,8,12, or 24$.
If you know the structure of $G:=Z_n^{\ast}$ (the multiplicative group of integers modulo $m$), the question becomes whether or not there exist elements of order $>2$. Indeed, in any group $G$, an element is its own inverse if and only if it is the identity or has order $2$. If you write $n$ as a product of primes $2^{r_0}p_1^{r_1} \cdots p_t^{r_t}$, then you have $$Z_n^{\ast} \cong Z_{2^{r_0}}^{\ast} \times Z_{p_1^{r_1}}^{\ast} \times \cdots \times Z_{p_t^{r_t}}^{\ast}$$ If any of the odd primes $p_i$ are $\neq 3$, then you're going to have elements of order $>2$. Indeed, for $p$ odd, $Z_{p^r}^{\ast}$ is cyclic of order $p^r - p^{r-1}$, so here you would have $p^r - p^{r-1} \geq 5^r - 5^{r-1} \geq 5^1 - 5^0 = 4$. Use the fact that a cyclic group of order $d$ always has an element of order $d$.
So $G$ has to look like $Z_{2^{r_0}}^{\ast} \times Z_{3^{r_1}}^{\ast}$ in order to have all elements be of order two. But if $r_1 > 1$, then $Z_{3^{r_1}}^{\ast}$ has at least $3^2 - 3 = 6$ elements, which is a no go. Finally, $Z_{2^{r_0}}^{\ast}$ (for $r_0 > 1$) is isomorphic to $\mathbb{Z}/2\mathbb{Z} \times \mathbb{Z}/2^{r_0-2} \mathbb{Z}$, which has clearly elements of order greater than $2$ if and only if $r_0 \geq 4$.
So $n$ has to be of the form $2^{r_0}3^{r_1}$, with $0 \leq r_0 \leq 3$ and $0 \leq r_1 \leq 1$, and $r_0$ and $r_1$ both can't be zero. This leaves exactly the possibilities you mentioned.
We want to find out for what $n$ it is true that if $\gcd(a,n)=1$ then $a^2\equiv 1\pmod{n}$.
Note that $n$ cannot be divisible by a prime $p\gt 3$. For the congruence $x^2\equiv 1\pmod{p}$ has only two solutions if $p$ is an odd prime. Thus if $\varphi(p)\gt 2$, there must be an $a$ not divisible by $p$ such that $a^2\not\equiv 1\pmod{p}$, and therefore $a^2\not\equiv 1\pmod{p^k}$, where $p^k$ is the highest power of $p$ that divides $n$. Using the Chinese Remainder Theorem, we can find a $b$ such that $b\equiv a \pmod{p^k}$ and $b\equiv 1\pmod{n/p^k}$. For this $b$ we have $\gcd(b,n)=1$ and $b^2\not\equiv 1\pmod{n}$.
So the only possible prime divisors of $n$ are $2$ and $3$. Note that $9$ cannot divide $n$, for $5^2\not\equiv 1\pmod{9}$.
Also, $16$ cannot divide $n$, because $5^2\not\equiv 1\pmod{16}$.
That leaves us as candidates $1,2,4,8$ and $3,6,12,24$. But $n=1$ is forbidden by the question. We can easily check that the others work.