Wrong answer,sum of arrays

my code:

i am getting wrong answers

You have to add the two arrays just like you would add two numbers. When you perform addition on numbers you have to start adding from the end, not the front. Moreover, there maybe a carryover also, so you have to take care of adding the carryover to the next set of elements. And the size of the resultant array could be greater than max of sizes of array1 and array2 if there is a carryover in the end. So, consider using an arraylist for that.

now i have modified my code as you said ā€¦my code is here:


it shows exception
Exception in thread ā€œmainā€ java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.set(ArrayList.java:472)
at fike.main(fike.java:44)

okay nowā€¦i get thatā€¦should use add instead of set!

Yes, exactly. Your code is perfect now, well done. Just need to change the ā€˜setā€™ to ā€˜addā€™. If you have no further queries about this question, then please mark the doubt as resolved :slight_smile:

can you tell me why i m getting infinite loop in this code of binary Search:

  1. Why have you made 2 main methods? Delete one of them.
  2. While calculating ā€˜midā€™, you need to use parenthesis otherwise division will be performed first and youā€™ll get wrong index. Change

int m =l+r/2;

to

int m =(l+r)/2;

sorry that main. happened when i was pasting code to editorā€¦

and i had studied somewhere that calculating do happens from left to right ā€¦mean first the leftmost operation is performed ā€¦is that wrong
if soā€¦then what the right way compiler performs operation ā€¦in what sequence
??

The operations will be performed following the usual BODMAS rules. So if you write l+r/2, it will perform division first and addition later.