the algorithm - easier solution