Help Rahul to Search

Why is my code showing TLE??

Hello @priyanshi.agarwal3405

To solve this what we can do is
Run a loop for all elements in array. At the end of every iteration, maintain following two values.
ones: The bits that have appeared 1st time or 4th time or 7th time … etc.
twos: The bits that have appeared 2nd time or 5th time or 8th time … etc.
Finally, we return the value of ‘ones’

How to maintain the values of ‘ones’ and ‘twos’?
‘ones’ and ‘twos’ are initialized as 0. For every new element in array, find out the common set bits in the new element and previous value of ‘ones’. These common set bits are actually the bits that should be added to ‘twos’. So do bitwise OR of the common set bits with ‘twos’. ‘twos’ also gets some extra bits that appear third time. These extra bits are removed later.
Update ‘ones’ by doing XOR of new element with previous value of ‘ones’. There may be some bits which appear 3rd time. These extra bits are also removed later.

Both ‘ones’ and ‘twos’ contain those extra bits which appear 3rd time. Remove these extra bits by finding out common set bits in ‘ones’ and ‘twos’.

Here is the code

If you didn’t understand anything just leave a reply to this thread and I will get back to you.

Sir, the code i wrote had complexity O(log n) still it showed TLE, and the code you shared has complexity O(n). So please tell how will TLE be resolved?

1 Like

Hello @priyanshi.agarwal3405

I provided you the above solution because you attached the question “Unique Number - |||” with this doubt.

As for this doubt, your if-else conditions are incorrect.
This testcase doesn’t work for your code. Try to dry run your code on this
3
3 1 2
3

Here is the modified code

If you need any help, just leave a reply to this thread and I will get back to you.

No sir, I didn’t attached any question. Don’t know how it got attached by itself. There must have been some confusion. My doubt is in question HELP RAHUL TO SEARCH of sorting challenges. My code link also I have shared with the doubt. It shows TLE.

Hello @priyanshi.agarwal3405

There might be some error at the backend.
As for the question “Help Rahul To Search”, I already replied you with the solution and the modified code. Your if-else conditions were incorrect.

If you need any help, just leave a reply to this thread and I will get back to you.

Okay sir, Thank you!!