Nagarro Interview Question

They just want you to justify your code properly and yes, the code should be in minimum time complexity.