You are getting a TLE because the complexity of your code is O(N^2). You could write a more optimized code with a complexity of O(N), i.e. with a single loop traversing the array instead of using two loops.
Hint: Since the question is asking about the length from each ‘x’ as start, you should traverse the array from the end, so that you can store the desired value for each index in an auxiliary array. Try using the length of alternate array traversed so far in one single iteration.
I can help you with the code as well, but it will be better for you to think of this approach once.