\begin{algorithm}
\caption{Counting Elements in a Given Interval}
\begin{algorithmic}
\INPUT a sequence $A$ of $n$ elements ranging from $0$ to $k$
\OUTPUT a procedure that can count the number of elements in interval $[a..b]$
\STATE let $C[0..k]$ be a new array initialized with all 0's
\FOR{$i = 1$ \TO $n$}
\STATE $C[A[i]] = C[A[i]] + 1$
\ENDFOR
\STATE \COMMENT{$C[i]$ now contains the number of elements equal to $i$}
\FOR{$i = 1$ \TO $k$}
\STATE $C[i] = C[i] + C[i-1]$
\ENDFOR
\STATE \COMMENT{$C[i]$ now contains the number of elements less than or equal to $i$}
\STATE
\PROCEDURE{IntervalCount}{$a, b$}
\RETURN $C[b] - C[a-1]$
\ENDPROCEDURE
\STATE \COMMENT{count the number of $A$'s elements in interval $[a..b]$ within $O(1)$}
\end{algorithmic}
\end{algorithm}