JPMorganChase Interview Question

Leet code medium level of difficulty