Amazon Interview Question

Solve a backtracking problem and TC.