started encoding algorithm for first case