Codility Solution