nice algorithm