This code: int checkLength = k % 2 == 0 ? k / 2 : k / 2 + 1; can instead be: int checkLength = (k + 1) % 2; This saves a branch.