Grab interview question

Medium Level DSA question in codility