Rakuten interview question

Standard leetcode style question with followups. Some math involved