strugled with algorithm