DSA Day 72/100

DSA Day 72/100

Topic: Matrix

Questions Successfully Completed: 1

1) Add two Matrix

Easy

Question
Input: n1 = 2, m1 = 3 A[][] = {{1, 2, 3}, {4, 5, 6}} n2 = 2, m2 = 3 B[][] = {{1, 3, 3}, {2, 3, 3}} Output: 2 5 6 6 8 9 Explanation: The summation matrix of A and B is: res[][] = {{2, 5, 6}, {6, 8, 9}} The output is generated by traversing each row sequentially.
  //Function to add two matrices.
    static int[][] sumMatrix(int A[][], int B[][])
    {
      int[][]result = new int[A.length][A[0].length];

        if(A.length==B.length && A[0].length==B[0].length)
        {

           for(int i=0;i<A.length;i++){
               for(int j=0;j<A[0].length;j++){
                   result[i][j] = A[i][j] + B[i][j];
               }
           }
        }
        else{
            result = new int[1][1];
            result[0][0] = -1;
        }
       return result;
    }

Thank you for reading :)