Samsung Research America Interview Question

Medium question from Leetcode - codility test