Visa Inc. Interview Question

2d matrix question from leetcode