Electronic Arts interview question

A leetcode medium problem supposed to be slove in 20mins.