Sponsored
Use these hints if you're stuck. Try solving on your own first.
First, solve the problem assuming <code>nums</code> contains distinct values.
Make a new array with each element being the pair <code>(nums[i], i)</code> for every <code>i</code> and call it <code>num_ind</code>.
Sort <code>num_ind</code> in decreasing order.
Iterate over <code>num_ind</code> and store a variable that represents the minimum index (i.e. min of <code>num_ind[i].second</code>) that has been iterated until now. Call it <code>min_index</code>
Now if you are currently on pair <code>(nums[x], x)</code>, then <code>ans = max(ans, min_index - x)</code>.
Now try to remove the first assumption.