Grid Dynamics Interview Question

it was leetcode mid problems