Applied Intuition Interview Question

Coding question with hard LC difficulty