Squarepoint Capital interview question

leetcode medium question (<1400cf difficulty)