Morgan Stanley interview question

A leetcode question under array topic.