Why is my code showing TLE??
Help Rahul to Search
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?
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!!