Ubisoft Interview Question

A leetcode hard problem about choosing and reverting.