Atlassian Interview Question

Leetcode question then improve its performance. Mention time complexity