+ 1

Hi, Could you please look into the logic of my java code and help me find what's wrong? It's failing for larger inputs.

I have created a program to find the difference between two arrays(assuming each array as a number). Assumptions: 1)each array is a number taking the value in the last position of the array as the one's digit of the number and last but one digit as 10's position of the number, so on. 2)2nd array/number is always greater than(or equal to) the 1st number. 3)Each index of the array contains only one digit between 0 to 9. My code: https://code.sololearn.com/cCC1WhL84v2j You can find the complete question with answer here(I am just looking to find the flaw in my code) : https://www.pepcoding.com/resources/online-java-foundation/function-and-arrays/difference_of_two_arrays/topic (You can ask me in your answer if you can't understand any part of my code)

9th Mar 2022, 5:14 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
10 Answers
+ 2
Jayakrishna🇮🇳 Thanks for going through my code. Actually the code is failing for larger values. For example I gave a1 as 6302 and a2 as 726300 and I got output as 720898 which should be 719998.
9th Mar 2022, 5:32 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 2
Line no: 44 use x-- before assigning like : while(x-- > 0) { a2[j-x]=a2[j-x]+9; //x--; }
9th Mar 2022, 7:02 PM
Jayakrishna 🇮🇳
+ 2
Thank you Jayakrishna🇮🇳 zemiak but it's failing when I take a1 as 9206183 and a2 as 400000010 and interestingly the current code is working for this set of values except that that it's giving the 1st digit wrong. I guess we are missing some particular case and hence there's some logical flaw in the code.
10th Mar 2022, 6:45 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 1
I think code works fine but only not Ingoring to print leading zeros if any.. If that not a constraint then it fine.
9th Mar 2022, 5:27 PM
Jayakrishna 🇮🇳
+ 1
As a general tip; use clear and descriptive variable names. It is so much easier for us (and for you in the long run) to write clear, descriptive variables. I have no clue what “a2” is supposed to do or be.
9th Mar 2022, 6:01 PM
Brave Tea
Brave Tea - avatar
+ 1
or while (--x > 0) {
10th Mar 2022, 7:03 AM
zemiak
+ 1
Iam getting correct only after that change... What your output?
10th Mar 2022, 7:22 PM
Jayakrishna 🇮🇳
+ 1
I tried code corrected above 7 9 2 0 6 1 8 3 9 4 0 0 0 0 0 0 1 0 and got 390793827 same as System.out.println(400_000_010 - 9_206_183); //390793827
10th Mar 2022, 9:57 PM
zemiak
+ 1
Jayakrishna🇮🇳 zemiak Thanks a lot to both of you🎉🎈. Yes, it's working fine now. I forgot to remove x--; in that while loop while changing the code from while (x>0) to (--x>O). In case you are interested, please have a look at another code I am having an issue with(its a little more complex than this one, feel free to ask in case you can't understand something there) https://www.sololearn.com/Discuss/2994989/could-you-please-look-into-the-logic-of-my-java-program-and-help-me-find-what-s-wrong-in-it
11th Mar 2022, 8:35 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar