MathWorks interview question

Explaining your code from the HackerRank challenge, analyzing runtime