Snippets Collections
class Solution
{
    //Function to add two matrices.
    static int[][] sumMatrix(int A[][], int B[][])
    {
        int n = A.length, m = A[0].length;
        int res[][] = new int[0][0];
        //Check if two input matrix are of different dimensions
        if(n != B.length || m != B[0].length)
            return res;
        
        res = new int[n][m];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                res[i][j] = A[i][j] + B[i][j];
                
        return res;
    }
}
star

Tue Feb 08 2022 08:09:29 GMT+0000 (UTC) https://practice.geeksforgeeks.org/problems/adding-two-matrices3512/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #addition #practice

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension