Qualcomm interview question

Standard leetcode medium question, not easy for sure.