Amazon Interview Question

How would you implement the code with less time and space complexity?

Amazon Interview Question: How would you implement the code with less time and space complexity? | Glassdoor