Matrix equation implies invertibility

Let $D = \operatorname{diag}(\lambda_1, \ldots, \lambda_n)$ be a diagonal matrix with positive entries $\lambda_i > 0$ (some of them might coincide). If we have the matrix equation $A D A^t = D$, does it follow that $A$ is invertible with $A^t = A^{-1}$?

It is obviously true if $A$ and $D$ commute. For $(2\times 2)$-matrices one can check by hand that the claim is true. I expect a counter-example for bigger matrices, but can not come up with one.

Solutions Collecting From Web of "Matrix equation implies invertibility"

Presumably all matrices here are real square matrices. Let $Q=D^{-1/2}AD^{1/2}$. Then the equation in question becomes $QQ^T=I$. Hence $Q$ must be real orthogonal and $A$ must be in the form of $D^{1/2}QD^{-1/2}$. Therefore, $A$ in invertible but it’s not necessarily orthogonal. For instance, consider
$$
D=\pmatrix{1\\ &2},\ Q=\frac1{\sqrt{2}}\pmatrix{1&-1\\ 1&1},
\ A=D^{1/2}QD^{-1/2}=\pmatrix{\frac1{\sqrt{2}}&-\frac12\\ 1&\frac1{\sqrt{2}}}.
$$