Implementation of daily programmer challenge 177