TLE please help

TLE exceeded please help

if your code is in Java please Mention it on Title of doubt
i can only help you with the approach
not the code

please post a new doubt for the same with mentioning Java Code in tiltle and mark this doubt as resolved

The idea is to use Binary Search Below is an observation in the input array.
All elements before the required have the first occurrence at even index (0, 2, …) and next occurrence at odd index (1, 3, …). And all elements after the required elements have the first occurrence at odd index and next occurrence at even index.

  1. Find the middle index, say ‘mid’.

  2. If ‘mid’ is even, then compare arr[mid] and arr[mid + 1]. If both are the same, then the required element after ‘mid’ else before mid.

  3. If ‘mid’ is odd, then compare arr[mid] and arr[mid – 1]. If both are the same, then the required element after ‘mid’ else before mid.