# DOR problem(JAVA IS A BUGGY LANGUAGE)

// Arrays.fill(aa, 1); for ( i = ma.length-1; i >=0; i–) { if (na[i]==ma[i]) { aa[i]=na[i]; } else break; } // for (; i >=0&&i>-1; i–) // aa[i]=1;

``````        System.out.println(binaryToDecimal(aa));
``````

if I am using second for loop then it gives tle

but when I use Arrays.fill(aa, 1); instead of second for loop which I have commented then id doesn’t give tle Idk why can you plz explain!

1 Like

Heavily Documented Implementation here: https://www.codechef.com/viewsolution/27526514

Some cliffnotes:

1. I extensively used >> and << operators.
2. 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.

2 Likes

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.

1 Like

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.

I’m gonna have a sleepless night.

1 Like

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.

I don’t think so, I am coding in java more than 2 years never found this situation. I know all the basics and it’s just a for -loop.

I think you should check that bug before saying this.

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.

2 Likes