Samsung Research America interview question

Medium question from Leetcode - codility test