MathWorks interview question

Medium Leetcode Question for Tech phone screen