Solving candy problem