I got a massive hint about solving the problem by doing the problem thanks to the fourth test case. (Although I solved it in practice).
I converted 1000000000123, 1000000123456, 1000000192511 to binary. Here I observed that the final answer in binary is as follows:
"111010001101010010100"“1111111111111111111”. That lead me to the pattern quickly that all common leading bits are to be retained and all bits that follow are to be set.
I got your logic, I did same by using array to store bits but my doubt is different about for loop if i use it then it gives tle but if i use Arrays.fill() function instead of for loop then it doesn’t give tle why? its not about logic of the question.
Interesting bug you have there. I’m still not sure what’s really going wrong here. However, I’ve got an AC by using the same logic you used in the code that received a TLE. Here’s the submission. Please play around with my code and try to figure out what the bug really is.
Now I am shifting to c++,
This is not the first time that I suffer like this in java that logic is correct but loop is going for infinite loop.
Last time as I remember on hacker Earth same problem occur and I know test cases at that time I debug for 2 hours to find out but at last the condition was false it should came out from loop but it didn’t instead it goes to infinite loop.
Java is buggy language Now I hate it.
It’s not. Probably you didn’t understand the language well. Yes, It is quite different from other languages but if u could understand its basics behind every logic. then you can handle these situations.
You’re getting TLE because you dont understand the language well. You are using Scanner for scanning your inputs, which is very slow. Use Fast I/O for java from gfg, you wont get any TLE on both of your codes
If it truly is because of the Scanner class, then can you please explain why the AC solution doesn’t get a TLE? In case you didn’t notice, even I used the Scanner class in my implementation right here and surprisingly got an AC! Of course, I know that using the Scanner class isn’t the best, but if the same logic doesn’t end up in a TLE, then why should it in this program? Please help debugging it. Thank you.