Index out of bound

im still having that error after putting M there

hey @missroy Can you send me your code so that I can look at the problem.

hey @missroy if your doubt is still not cleared then send me the code I will make the concept Clear. other please Mark this doubt as Resolved.

public class AddTwoArrays { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M=sc.nextInt(); int[] arr1=new int[N]; for(int i=0;i<N;i++) { arr1[i]=sc.nextInt(); } int[] arr2=new int[M]; for(int i=0;i<M;i++) { arr2[i]=sc.nextInt(); } int[] sumarray=new int[N]; for(int i=0;i<N;i++) { sumarray[i]=arr1[i]+arr2[i]; } for(int val:sumarray) { System.out.println(val); } } }

hey @missroy(I have given you the solution to this problem at the end of reply) The question says print the sum of two array not the sum of every element.
For eg =
if two arrays are:
arr1 = 1,2,3,4
arr2 = 5,4,3
the output should be = 1234 + 543 = 1777
then your output should look like this
1,7,7,7
but your code will give array out of bound exception since you are trying to print 4th element of arr2 which is not present .

the correct approach to this question will : For the given sample case , arr1=[1, 0, 2, 9] and arr2=[3, 4, 5, 6, 7] .Each element of both the arrays is a digit .Thus, both array forms two separate numbers and we need to calculate the sum of these two numbers.

Take an ans array (whose size is equal to the (max size of the two arrays +1 )) which will store the final number formed.

Start adding both the numbers digit by digit from the end i.e. 9+7=16 thus 6 is stored in the ans array at last index (as we are updating our array from last) and 1 is taken as carry.Thus, this process is repeated for every digit and at the end if carry is not 0 it is stored at the starting index of the ans array.First digit of the ans array represent carry over which is zero in this case. So ans is [3, 5, 5, 9, 6].

Algorithm:

1.Traverse both the arrays simultaneously from the end until we reach the 0th index of either of the array.
2.While traversing each element of array, add an element from both arrays and carry from the previous sum.
…………2.1 Store the unit digit of the sum(obtained by doing - sum%10) in the ans array.
…………2.2 forward carry(obtained by doing - sum/10) for the next index sum.
3.After the loop ends we are left with one of the arrays and the carry.
4.We will now repeat step 2 but now for one array which is left after the loop.
5.If carry !=0 then store the carry in the 0th index of ans array.
6.Print the ans array .

Code

 public static void arraySum(int[] one, int[] two) {

        ArrayList ans = new ArrayList<>();

        int i = one.length - 1;
        int j = two.length - 1;

        int carry = 0;
        while (i >= 0 || j >= 0) {

            int sum = carry;

            if (i >= 0) {
                sum += one[i];
            }

            if (j >= 0) {
                sum += two[j];
            }

            int rem = sum % 10;
            ans.add(0, rem);
            carry = sum / 10;

            i--;
            j--;
        }

        if (carry != 0) {
            ans.add(0, carry);
        }

        for (i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i) + ", ");
        }
        System.out.println("END");
    }

``

``

is this code for sum of two arrays

Yes it it is @missroy

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.