I have written this code to formulate the algorithm for merging two arrays, but I get an error.
\begin{algorithm}[t]
\caption{Serial Merge}
\label{alg:insert}
\begin{algorithmic}[1]
\Require Two sorted arrays $A$ and $B$
\Ensure Sorted Array $C$
\State $lA \leftarrow length(A)$
\State $lB \leftarrow length(B)$
\State $x \leftarrow 0$
\State $y \leftarrow 0$
\State $z \leftarrow 0$
\While { $x \neq lA$ and $y \neq lB$ }
\If { $A[x] < $B[y]$}
\State $C[z++] \leftarrow A[x++]$
\Else
\State $C[z++] \leftarrow B[y++]$
\EndIf
\EndWhile
\While { $x \neq lA$ }
\State $C[z++] \leftarrow A[x++]$
\EndWhile
\While { $y \neq lB$ }
\State $C[z++] \leftarrow B[y++]$
\EndWhile
\end{algorithmic}
\end{algorithm}
Error:
! LaTeX Error: Command \item invalid in math mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.15 \State
$C[z++] \leftarrow A[x++]$
?
It works if I remove the surrounding if else statements. What am I doing wrong?

