Amazon Interview Question

What are the time and space complexity of this problem?