Subarray with distinct elements: TLE on last two cases

I am getting the TLE for last 2 cases in this question.

Here is my code:

How can I optimize it?