Intel Corporation Interview Question

MEX question leetcode medium, follow up hard