The Wayback Machine - https://web.archive.org/web/20241006082211/https://www.geeksforgeeks.org/operations-sparse-matrices/
Open In App

Operations on Sparse Matrices

Last Updated : 02 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given two sparse matrices (Sparse Matrix and its representations | Set 1 (Using Arrays and Linked Lists)), perform operations such as add, multiply or transpose of the matrices in their sparse form itself. The result should consist of three sparse matrices, one obtained by adding the two input matrices, one by multiplying the two matrices and one obtained by transpose of the first matrix. 

Example: Note that other entries of matrices will be zero as matrices are sparse.

Input : 

Matrix 1: (4x4)
Row Column Value
1   2       10
1   4       12
3   3       5
4   1       15
4   2       12

Matrix 2: (4X4)
Row Column Value
1   3       8
2   4       23
3   3       9
4   1       20
4   2       25

Output :

Result of Addition: (4x4)
Row Column Value
1   2      10
1   3      8
1   4      12
2   4      23
3   3      14
4   1      35
4   2      37

Result of Multiplication: (4x4)
Row Column Value
1   1      240
1   2      300
1   4      230
3   3      45
4   3      120
4   4      276

Result of transpose on the first matrix: (4x4)
Row Column Value
1   4      15
2   1      10
2   4      12
3   3      5
4   1      12

The sparse matrix used anywhere in the program is sorted according to its row values. Two elements with the same row values are further sorted according to their column values. 

Now to Add the matrices, we simply traverse through both matrices element by element and insert the smaller element (one with smaller row and col value) into the resultant matrix. If we come across an element with the same row and column value, we simply add their values and insert the added data into the resultant matrix. 

To Transpose a matrix, we can simply change every column value to the row value and vice-versa, however, in this case, the resultant matrix won’t be sorted as we require. Hence, we initially determine the number of elements less than the current element’s column being inserted in order to get the exact index of the resultant matrix where the current element should be placed. This is done by maintaining an array index[] whose ith value indicates the number of elements in the matrix less than the column i. 

To Multiply the matrices, we first calculate transpose of the second matrix to simplify our comparisons and maintain the sorted order. So, the resultant matrix is obtained by traversing through the entire length of both matrices and summing the appropriate multiplied values. 

Any row value equal to x in the first matrix and row value equal to y in the second matrix (transposed one) will contribute towards result[x][y]. This is obtained by multiplying all such elements having col value in both matrices and adding only those with the row as x in first matrix and row as y in the second transposed matrix to get the result[x][y]. 

For example: Consider 2 matrices:

Row Col Val      Row Col Val
1   2   10       1   1   2
1   3   12       1   2   5
2   1   1        2   2   1
2   3   2        3   1   8

The resulting matrix after multiplication will be obtained as follows:

Transpose of second matrix:

Row Col Val      Row Col Val
1   2   10       1   1   2
1   3   12       1   3   8
2   1   1        2   1   5
2   3   2        2   2   1

Summation of multiplied values:

result[1][1] = A[1][3]*B[1][3] = 12*8 = 96
result[1][2] = A[1][2]*B[2][2] = 10*1 = 10
result[2][1] = A[2][1]*B[1][1] + A[2][3]*B[1][3] = 2*1 + 2*8 = 18
result[2][2] = A[2][1]*B[2][1] = 1*5 = 5

Any other element cannot be obtained 
by any combination of row in 
Matrix A and Row in Matrix B.

Hence the final resultant matrix will be:
 
Row Col Val 
1   1   96 
1   2   10 
2   1   18  
2   2   5  

Following is the implementation of above approach: 

C++




// C++ code to perform add, multiply
// and transpose on sparse matrices
#include <iostream>
using namespace std;
 
class sparse_matrix
{
 
    // Maximum number of elements in matrix
    const static int MAX = 100;
 
    // Double-pointer initialized by
    // the constructor to store
    // the triple-represented form
    int **data;
 
    // dimensions of matrix
    int row, col;
 
    // total number of elements in matrix
    int len;
 
public:
    sparse_matrix(int r, int c)
    {
 
        // initialize row
        row = r;
 
        // initialize col
        col = c;
 
        // initialize length to 0
        len = 0;
 
        //Array of Pointer to make a matrix
        data = new int *[MAX];
 
        // Array representation
        // of sparse matrix
        //[,0] represents row
        //[,1] represents col
        //[,2] represents value
        for (int i = 0; i < MAX; i++)
            data[i] = new int[3];
    }
 
    // insert elements into sparse matrix
    void insert(int r, int c, int val)
    {
 
        // invalid entry
        if (r > row || c > col)
        {
            cout << "Wrong entry";
        }
        else
        {
 
            // insert row value
            data[len][0] = r;
 
            // insert col value
            data[len][1] = c;
 
            // insert element's value
            data[len][2] = val;
 
            // increment number of data in matrix
            len++;
        }
    }
 
    void add(sparse_matrix b)
    {
 
        // if matrices don't have same dimensions
        if (row != b.row || col != b.col)
        {
            cout << "Matrices can't be added";
        }
 
        else
        {
            int apos = 0, bpos = 0;
            sparse_matrix result(row, col);
 
            while (apos < len && bpos < b.len)
            {
 
                // if b's row and col is smaller
                if (data[apos][0] > b.data[bpos][0] ||
                   (data[apos][0] == b.data[bpos][0] &&
                    data[apos][1] > b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(b.data[bpos][0],
                                  b.data[bpos][1],
                                  b.data[bpos][2]);
 
                    bpos++;
                }
 
                // if a's row and col is smaller
                else if (data[apos][0] < b.data[bpos][0] ||
                        (data[apos][0] == b.data[bpos][0] &&
                         data[apos][1] < b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(data[apos][0],
                                  data[apos][1],
                                  data[apos][2]);
 
                    apos++;
                }
 
                else
                {
 
                    // add the values as row and col is same
                    int addedval = data[apos][2] +
                                 b.data[bpos][2];
 
                    if (addedval != 0)
                        result.insert(data[apos][0],
                                      data[apos][1],
                                      addedval);
                    // then insert
                    apos++;
                    bpos++;
                }
            }
 
            // insert remaining elements
            while (apos < len)
                result.insert(data[apos][0],
                              data[apos][1],
                              data[apos++][2]);
 
            while (bpos < b.len)
                result.insert(b.data[bpos][0],
                              b.data[bpos][1],
                              b.data[bpos++][2]);
 
            // print result
            result.print();
        }
    }
 
    sparse_matrix transpose()
    {
 
        // new matrix with inversed row X col
        sparse_matrix result(col, row);
 
        // same number of elements
        result.len = len;
 
        // to count number of elements in each column
        int *count = new int[col + 1];
 
        // initialize all to 0
        for (int i = 1; i <= col; i++)
            count[i] = 0;
 
        for (int i = 0; i < len; i++)
            count[data[i][1]]++;
 
        int *index = new int[col + 1];
 
        // to count number of elements having
        // col smaller than particular i
 
        // as there is no col with value < 0
        index[0] = 0;
 
        // initialize rest of the indices
        for (int i = 1; i <= col; i++)
 
            index[i] = index[i - 1] + count[i - 1];
 
        for (int i = 0; i < len; i++)
        {
 
            // insert a data at rpos and
            // increment its value
            int rpos = index[data[i][1]]++;
 
            // transpose row=col
            result.data[rpos][0] = data[i][1];
 
            // transpose col=row
            result.data[rpos][1] = data[i][0];
 
            // same value
            result.data[rpos][2] = data[i][2];
        }
 
        // the above method ensures
        // sorting of transpose matrix
        // according to row-col value
        return result;
    }
 
    void multiply(sparse_matrix b)
    {
        if (col != b.row)
        {
 
            // Invalid multiplication
            cout << "Can't multiply, Invalid dimensions";
            return;
        }
 
        // transpose b to compare row
        // and col values and to add them at the end
        b = b.transpose();
        int apos, bpos;
 
        // result matrix of dimension row X b.col
        // however b has been transposed,
        // hence row X b.row
        sparse_matrix result(row, b.row);
 
        // iterate over all elements of A
        for (apos = 0; apos < len;)
        {
 
            // current row of result matrix
            int r = data[apos][0];
 
            // iterate over all elements of B
            for (bpos = 0; bpos < b.len;)
            {
 
                // current column of result matrix
                // data[,0] used as b is transposed
                int c = b.data[bpos][0];
 
                // temporary pointers created to add all
                // multiplied values to obtain current
                // element of result matrix
                int tempa = apos;
                int tempb = bpos;
 
                int sum = 0;
 
                // iterate over all elements with
                // same row and col value
                // to calculate result[r]
                while (tempa < len && data[tempa][0] == r &&
                       tempb < b.len && b.data[tempb][0] == c)
                {
                    if (data[tempa][1] < b.data[tempb][1])
 
                        // skip a
                        tempa++;
 
                    else if (data[tempa][1] > b.data[tempb][1])
 
                        // skip b
                        tempb++;
                    else
 
                        // same col, so multiply and increment
                        sum += data[tempa++][2] *
                             b.data[tempb++][2];
                }
 
                // insert sum obtained in result[r]
                // if its not equal to 0
                if (sum != 0)
                    result.insert(r, c, sum);
 
                while (bpos < b.len &&
                       b.data[bpos][0] == c)
 
                    // jump to next column
                    bpos++;
            }
            while (apos < len && data[apos][0] == r)
 
                // jump to next row
                apos++;
        }
        result.print();
    }
 
    // printing matrix
    void print()
    {
        cout << "\nDimension: " << row << "x" << col;
        cout << "\nSparse Matrix: \nRow\tColumn\tValue\n";
 
        for (int i = 0; i < len; i++)
        {
            cout << data[i][0] << "\t " << data[i][1]
                 << "\t " << data[i][2] << endl;
        }
    }
};
 
// Driver Code
int main()
{
 
    // create two sparse matrices and insert values
    sparse_matrix a(4, 4);
    sparse_matrix b(4, 4);
 
    a.insert(1, 2, 10);
    a.insert(1, 4, 12);
    a.insert(3, 3, 5);
    a.insert(4, 1, 15);
    a.insert(4, 2, 12);
    b.insert(1, 3, 8);
    b.insert(2, 4, 23);
    b.insert(3, 3, 9);
    b.insert(4, 1, 20);
    b.insert(4, 2, 25);
 
    // Output result
    cout << "Addition: ";
    a.add(b);
    cout << "\nMultiplication: ";
    a.multiply(b);
    cout << "\nTranspose: ";
    sparse_matrix atranspose = a.transpose();
    atranspose.print();
}
 
// This code is contributed
// by Bharath Vignesh J K


Java




// Java code to perform add,
// multiply and transpose on sparse matrices
 
public class sparse_matrix {
 
    // Maximum number of elements in matrix
    int MAX = 100;
 
    // Array representation
    // of sparse matrix
    //[][0] represents row
    //[][1] represents col
    //[][2] represents value
    int data[][] = new int[MAX][3];
 
    // dimensions of matrix
    int row, col;
 
    // total number of elements in matrix
    int len;
 
    public sparse_matrix(int r, int c)
    {
 
        // initialize row
        row = r;
 
        // initialize col
        col = c;
 
        // initialize length to 0
        len = 0;
    }
 
    // insert elements into sparse matrix
    public void insert(int r, int c, int val)
    {
 
        // invalid entry
        if (r > row || c > col) {
            System.out.println("Wrong entry");
        }
 
        else {
 
            // insert row value
            data[len][0] = r;
 
            // insert col value
            data[len][1] = c;
 
            // insert element's value
            data[len][2] = val;
 
            // increment number of data in matrix
            len++;
        }
    }
 
    public void add(sparse_matrix b)
    {
 
        // if matrices don't have same dimensions
        if (row != b.row || col != b.col) {
            System.out.println("Matrices can't be added");
        }
 
        else {
 
            int apos = 0, bpos = 0;
            sparse_matrix result = new sparse_matrix(row, col);
 
            while (apos < len && bpos < b.len) {
 
                // if b's row and col is smaller
                if (data[apos][0] > b.data[bpos][0] ||
                  (data[apos][0] == b.data[bpos][0] &&
                   data[apos][1] > b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(b.data[bpos][0],
                                  b.data[bpos][1],
                                  b.data[bpos][2]);
 
                    bpos++;
                }
 
                // if a's row and col is smaller
                else if (data[apos][0] < b.data[bpos][0] ||
                (data[apos][0] == b.data[bpos][0] &&
                  data[apos][1] < b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(data[apos][0],
                                  data[apos][1],
                                  data[apos][2]);
 
                    apos++;
                }
 
                else {
 
                    // add the values as row and col is same
                    int addedval = data[apos][2] + b.data[bpos][2];
 
                    if (addedval != 0)
                        result.insert(data[apos][0],
                                      data[apos][1],
                                      addedval);
                    // then insert
                    apos++;
                    bpos++;
                }
            }
 
            // insert remaining elements
            while (apos < len)
                result.insert(data[apos][0],
                              data[apos][1],
                              data[apos++][2]);
 
            while (bpos < b.len)
                result.insert(b.data[bpos][0],
                              b.data[bpos][1],
                              b.data[bpos++][2]);
 
            // print result
            result.print();
        }
    }
 
    public sparse_matrix transpose()
    {
 
        // new matrix with inversed row X col
        sparse_matrix result = new sparse_matrix(col, row);
 
        // same number of elements
        result.len = len;
 
        // to count number of elements in each column
        int count[] = new int[col + 1];
 
        // initialize all to 0
        for (int i = 1; i <= col; i++)
            count[i] = 0;
 
        for (int i = 0; i < len; i++)
            count[data[i][1]]++;
 
        int[] index = new int[col + 1];
 
        // to count number of elements having col smaller
        // than particular i
 
        // as there is no col with value < 1
        index[1] = 0;
 
        // initialize rest of the indices
        for (int i = 2; i <= col; i++)
 
            index[i] = index[i - 1] + count[i - 1];
 
        for (int i = 0; i < len; i++) {
 
            // insert a data at rpos and increment its value
            int rpos = index[data[i][1]]++;
 
            // transpose row=col
            result.data[rpos][0] = data[i][1];
 
            // transpose col=row
            result.data[rpos][1] = data[i][0];
 
            // same value
            result.data[rpos][2] = data[i][2];
        }
 
        // the above method ensures
        // sorting of transpose matrix
        // according to row-col value
        return result;
    }
 
    public void multiply(sparse_matrix b)
    {
 
        if (col != b.row) {
 
            // Invalid multiplication
            System.out.println("Can't multiply, "
                               + "Invalid dimensions");
 
            return;
        }
 
        // transpose b to compare row
        // and col values and to add them at the end
        b = b.transpose();
        int apos, bpos;
 
        // result matrix of dimension row X b.col
        // however b has been transposed, hence row X b.row
        sparse_matrix result = new sparse_matrix(row, b.row);
 
        // iterate over all elements of A
        for (apos = 0; apos < len;) {
 
            // current row of result matrix
            int r = data[apos][0];
 
            // iterate over all elements of B
            for (bpos = 0; bpos < b.len;) {
 
                // current column of result matrix
                // data[][0] used as b is transposed
                int c = b.data[bpos][0];
 
                // temporary pointers created to add all
                // multiplied values to obtain current
                // element of result matrix
                int tempa = apos;
                int tempb = bpos;
 
                int sum = 0;
 
                // iterate over all elements with
                // same row and col value
                // to calculate result[r]
                while (tempa < len && data[tempa][0] == r
                       && tempb < b.len && b.data[tempb][0] == c) {
 
                    if (data[tempa][1] < b.data[tempb][1])
 
                        // skip a
                        tempa++;
 
                    else if (data[tempa][1] > b.data[tempb][1])
 
                        // skip b
                        tempb++;
                    else
 
                        // same col, so multiply and increment
                        sum += data[tempa++][2] * b.data[tempb++][2];
                }
 
                // insert sum obtained in result[r]
                // if its not equal to 0
                if (sum != 0)
                    result.insert(r, c, sum);
 
                while (bpos < b.len && b.data[bpos][0] == c)
 
                    // jump to next column
                    bpos++;
            }
 
            while (apos < len && data[apos][0] == r)
 
                // jump to next row
                apos++;
        }
 
        result.print();
    }
 
    // printing matrix
    public void print()
    {
        System.out.println("Dimension: " + row + "x" + col);
        System.out.println("Sparse Matrix: \nRow Column Value");
 
        for (int i = 0; i < len; i++) {
 
            System.out.println(data[i][0] + " "
                             + data[i][1] + " " + data[i][2]);
        }
    }
 
    public static void main(String args[])
    {
 
        // create two sparse matrices and insert values
        sparse_matrix a = new sparse_matrix(4, 4);
        sparse_matrix b = new sparse_matrix(4, 4);
 
        a.insert(1, 2, 10);
        a.insert(1, 4, 12);
        a.insert(3, 3, 5);
        a.insert(4, 1, 15);
        a.insert(4, 2, 12);
        b.insert(1, 3, 8);
        b.insert(2, 4, 23);
        b.insert(3, 3, 9);
        b.insert(4, 1, 20);
        b.insert(4, 2, 25);
 
        // Output result
        System.out.println("Addition: ");
        a.add(b);
        System.out.println("\nMultiplication: ");
        a.multiply(b);
        System.out.println("\nTranspose: ");
        sparse_matrix atranspose = a.transpose();
        atranspose.print();
    }
}
 
// This code is contributed by Sudarshan Khasnis


Python3




# Python3 code to perform add,
# multiply and transpose on sparse matrices
class sparse_matrix :
 
    def __init__(self, r, c):
     
        # Maximum number of elements in matrix
        self.MAX = 100;
         
        # Array representation
        # of sparse matrix
        #[][0] represents row
        #[][1] represents col
        #[][2] represents value
        self.data = [None for _ in range(self.MAX)]
        for i in range(self.MAX):
            self.data[i] = [None for _ in range(3)]
 
        # dimensions of matrix
        self.row = r;
        self.col = c;
 
        # total number of elements in matrix
        self.len = 0;
     
    # insert elements into sparse matrix
    def insert(self, r, c, val):
     
        # invalid entry
        if (r > self.row or c > self.col) :
            print("Wrong entry");
        else :
 
            # insert row value
            self.data[self.len][0] = r;
 
            # insert col value
            self.data[self.len][1] = c;
 
            # insert element's value
            self.data[self.len][2] = val;
 
            # increment number of data in matrix
            self.len += 1;
         
    def add(self, b):
 
        # if matrices don't have same dimensions
        if (self.row != b.row or self.col != b.col) :
            print("Matrices can't be added");
        else:
 
            apos = 0;
            bpos = 0;
            result = sparse_matrix(self.row, self.col);
 
            while (apos < self.len and bpos < b.len):
 
                # if b's row and col is smaller
                if (self.data[apos][0] > b.data[bpos][0] or (self.data[apos][0] == b.data[bpos][0] and self.data[apos][1] > b.data[bpos][1])):
 
                    # insert smaller value into result
                    result.insert(b.data[bpos][0],
                                  b.data[bpos][1],
                                  b.data[bpos][2]);
                    bpos += 1
                 
                # if a's row and col is smaller
                elif (self.data[apos][0] < b.data[bpos][0] or (self.data[apos][0] == b.data[bpos][0] and self.data[apos][1] < b.data[bpos][1])):
                     
                    # insert smaller value into result
                    result.insert(self.data[apos][0], self.data[apos][1], self.data[apos][2]);
                    apos += 1;
             
                else:
 
                    # add the values as row and col is same
                    addedval = self.data[apos][2] + b.data[bpos][2];
 
                    if (addedval != 0):
                        result.insert(self.data[apos][0], self.data[apos][1], addedval);
                    # then insert
                    apos += 1;
                    bpos += 1;
                 
            # insert remaining elements
            while (apos < self.len):
                result.insert(self.data[apos][0],self.data[apos][1], self.data[apos][2]);
                apos += 1
 
            while (bpos < b.len):
                result.insert(b.data[bpos][0], b.data[bpos][1], b.data[bpos][2]);
                bpos += 1
 
            # print result
            result.print();
         
    def transpose(self):
     
        # new matrix with inversed row X col
        result = sparse_matrix(self.col, self.row);
 
        # same number of elements
        result.len = self.len;
 
        # to count number of elements in each column
        count = [None for _ in range(self.col + 1)];
 
        # initialize all to 0
        for i in range(1, 1 + self.col):
            count[i] = 0;
 
        for i in range(0, self.len):
            count[self.data[i][1]] += 1
 
        index = [None for _ in range(self.col + 1)]
 
        # to count number of elements having col smaller
        # than particular i
 
        # as there is no col with value < 1
        index[1] = 0;
 
        # initialize rest of the indices
        for i in range(2, 1 + self.col):
            index[i] = index[i - 1] + count[i - 1];
 
        for i in range(self.len):
 
            # insert a data at rpos and increment its value
            rpos = index[self.data[i][1]]
            index[self.data[i][1]] += 1
 
            # transpose row=col
            result.data[rpos][0] = self.data[i][1];
 
            # transpose col=row
            result.data[rpos][1] = self.data[i][0];
 
            # same value
            result.data[rpos][2] = self.data[i][2];
         
        # the above method ensures
        # sorting of transpose matrix
        # according to row-col value
        return result;
     
    def multiply(self, b):
        if (self.col != b.row):
 
            # Invalid multiplication
            print("Can't multiply, Invalid dimensions");
            return;
         
        # transpose b to compare row
        # and col values and to add them at the end
        b = b.transpose();
 
        # result matrix of dimension row X b.col
        # however b has been transposed, hence row X b.row
        result = sparse_matrix(self.row, b.row);
 
        # iterate over all elements of A
        for apos in range(self.len):
 
            # current row of result matrix
            r = self.data[apos][0];
 
            # iterate over all elements of B
            for bpos in range(b.len):
 
                # current column of result matrix
                # data[][0] used as b is transposed
                c = b.data[bpos][0];
 
                # temporary pointers created to add all
                # multiplied values to obtain current
                # element of result matrix
                tempa = apos;
                tempb = bpos;
                sum = 0;
 
                # iterate over all elements with
                # same row and col value
                # to calculate result[r]
                while (tempa < self.len and self.data[tempa][0] == r and tempb < b.len and b.data[tempb][0] == c):
 
                    if (self.data[tempa][1] < b.data[tempb][1]):
 
                        # skip a
                        tempa += 1
 
                    elif (self.data[tempa][1]  > b.data[tempb][1]):
 
                        # skip b
                        tempb += 1
                    else:
 
                        # same col, so multiply and
                        # increment
                        sum += self.data[tempa][2] * b.data[tempb][2];
                        tempa += 1
                        tempb += 1
                 
                # insert sum obtained in result[r]
                # if its not equal to 0
                if (sum != 0):
                    result.insert(r, c, sum);
                while (bpos < b.len and b.data[bpos][0] == c):
 
                    # jump to next column
                    bpos += 1
             
            while (apos < self.len and self.data[apos][0] == r):
 
                # jump to next row
                apos += 1
         
        result.print();
     
    # printing matrix
    def print(self):
        print("Dimension:", self.row, "x", self.col);
        print("Sparse Matrix: \nRow Column Value");
     
        for i in range(self.len):
            print(self.data[i][0], self.data[i][1], self.data[i][2]);
         
# create two sparse matrices and insert values
a = sparse_matrix(4, 4);
b = sparse_matrix(4, 4);
 
a.insert(1, 2, 10);
a.insert(1, 4, 12);
a.insert(3, 3, 5);
a.insert(4, 1, 15);
a.insert(4, 2, 12);
b.insert(1, 3, 8);
b.insert(2, 4, 23);
b.insert(3, 3, 9);
b.insert(4, 1, 20);
b.insert(4, 2, 25);
 
# Output result
print("Addition: ");
a.add(b);
print("\nMultiplication: ");
a.multiply(b);
print("\nTranspose: ");
atranspose = a.transpose();
atranspose.print();
 
# This code is contributed by phasing17


C#




// C# code to perform add,
// multiply and transpose on sparse matrices
 
public class sparse_matrix {
 
    // Maximum number of elements in matrix
    static int MAX = 100;
 
    // Array representation
    // of sparse matrix
    //[,0] represents row
    //[,1] represents col
    //[,2] represents value
    int[,] data = new int[MAX,3];
 
    // dimensions of matrix
    int row, col;
 
    // total number of elements in matrix
    int len;
 
    public sparse_matrix(int r, int c)
    {
 
        // initialize row
        row = r;
 
        // initialize col
        col = c;
 
        // initialize length to 0
        len = 0;
    }
 
    // insert elements into sparse matrix
    public void insert(int r, int c, int val)
    {
 
        // invalid entry
        if (r > row || c > col) {
            System.Console.WriteLine("Wrong entry");
        }
 
        else {
 
            // insert row value
            data[len,0] = r;
 
            // insert col value
            data[len,1] = c;
 
            // insert element's value
            data[len,2] = val;
 
            // increment number of data in matrix
            len++;
        }
    }
 
    public void add(sparse_matrix b)
    {
 
        // if matrices don't have same dimensions
        if (row != b.row || col != b.col) {
            System.Console.WriteLine("Matrices can't be added");
        }
 
        else {
 
            int apos = 0, bpos = 0;
            sparse_matrix result = new sparse_matrix(row, col);
 
            while (apos < len && bpos < b.len) {
 
                // if b's row and col is smaller
                if (data[apos,0] > b.data[bpos,0] ||
                (data[apos,0] == b.data[bpos,0] &&
                data[apos,1] > b.data[bpos,1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(b.data[bpos,0],
                                b.data[bpos,1],
                                b.data[bpos,2]);
 
                    bpos++;
                }
 
                // if a's row and col is smaller
                else if (data[apos,0] < b.data[bpos,0] ||
                (data[apos,0] == b.data[bpos,0] &&
                data[apos,1] < b.data[bpos,1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(data[apos,0],
                                data[apos,1],
                                data[apos,2]);
 
                    apos++;
                }
 
                else {
 
                    // add the values as row and col is same
                    int addedval = data[apos,2] + b.data[bpos,2];
 
                    if (addedval != 0)
                        result.insert(data[apos,0],
                                    data[apos,1],
                                    addedval);
                    // then insert
                    apos++;
                    bpos++;
                }
            }
 
            // insert remaining elements
            while (apos < len)
                result.insert(data[apos,0],
                            data[apos,1],
                            data[apos++,2]);
 
            while (bpos < b.len)
                result.insert(b.data[bpos,0],
                            b.data[bpos,1],
                            b.data[bpos++,2]);
 
            // print result
            result.print();
        }
    }
 
    public sparse_matrix transpose()
    {
 
        // new matrix with inversed row X col
        sparse_matrix result = new sparse_matrix(col, row);
 
        // same number of elements
        result.len = len;
 
        // to count number of elements in each column
        int[] count = new int[col + 1];
 
        // initialize all to 0
        for (int i = 1; i <= col; i++)
            count[i] = 0;
 
        for (int i = 0; i < len; i++)
            count[data[i,1]]++;
 
        int[] index = new int[col + 1];
 
        // to count number of elements having col smaller
        // than particular i
 
        // as there is no col with value < 1
        index[1] = 0;
 
        // initialize rest of the indices
        for (int i = 2; i <= col; i++)
 
            index[i] = index[i - 1] + count[i - 1];
 
        for (int i = 0; i < len; i++) {
 
            // insert a data at rpos and increment its value
            int rpos = index[data[i,1]]++;
 
            // transpose row=col
            result.data[rpos,0] = data[i,1];
 
            // transpose col=row
            result.data[rpos,1] = data[i,0];
 
            // same value
            result.data[rpos,2] = data[i,2];
        }
 
        // the above method ensures
        // sorting of transpose matrix
        // according to row-col value
        return result;
    }
 
    public void multiply(sparse_matrix b)
    {
 
        if (col != b.row) {
 
            // Invalid multiplication
            System.Console.WriteLine("Can't multiply, "
                            + "Invalid dimensions");
 
            return;
        }
 
        // transpose b to compare row
        // and col values and to add them at the end
        b = b.transpose();
        int apos, bpos;
 
        // result matrix of dimension row X b.col
        // however b has been transposed, hence row X b.row
        sparse_matrix result = new sparse_matrix(row, b.row);
 
        // iterate over all elements of A
        for (apos = 0; apos < len;) {
 
            // current row of result matrix
            int r = data[apos,0];
 
            // iterate over all elements of B
            for (bpos = 0; bpos < b.len;) {
 
                // current column of result matrix
                // data[,0] used as b is transposed
                int c = b.data[bpos,0];
 
                // temporary pointers created to add all
                // multiplied values to obtain current
                // element of result matrix
                int tempa = apos;
                int tempb = bpos;
 
                int sum = 0;
 
                // iterate over all elements with
                // same row and col value
                // to calculate result[r]
                while (tempa < len && data[tempa,0] == r
                    && tempb < b.len && b.data[tempb,0] == c) {
 
                    if (data[tempa,1] < b.data[tempb,1])
 
                        // skip a
                        tempa++;
 
                    else if (data[tempa,1] > b.data[tempb,1])
 
                        // skip b
                        tempb++;
                    else
 
                        // same col, so multiply and increment
                        sum += data[tempa++,2] * b.data[tempb++,2];
                }
 
                // insert sum obtained in result[r]
                // if its not equal to 0
                if (sum != 0)
                    result.insert(r, c, sum);
 
                while (bpos < b.len && b.data[bpos,0] == c)
 
                    // jump to next column
                    bpos++;
            }
 
            while (apos < len && data[apos,0] == r)
 
                // jump to next row
                apos++;
        }
 
        result.print();
    }
 
    // printing matrix
    public void print()
    {
        System.Console.WriteLine("Dimension: " + row + "x" + col);
        System.Console.WriteLine("Sparse Matrix: \nRow Column Value");
 
        for (int i = 0; i < len; i++) {
 
            System.Console.WriteLine(data[i,0] + " "
                            + data[i,1] + " " + data[i,2]);
        }
    }
 
    public static void Main()
    {
 
        // create two sparse matrices and insert values
        sparse_matrix a = new sparse_matrix(4, 4);
        sparse_matrix b = new sparse_matrix(4, 4);
 
        a.insert(1, 2, 10);
        a.insert(1, 4, 12);
        a.insert(3, 3, 5);
        a.insert(4, 1, 15);
        a.insert(4, 2, 12);
        b.insert(1, 3, 8);
        b.insert(2, 4, 23);
        b.insert(3, 3, 9);
        b.insert(4, 1, 20);
        b.insert(4, 2, 25);
 
        // Output result
        System.Console.WriteLine("Addition: ");
        a.add(b);
        System.Console.WriteLine("\nMultiplication: ");
        a.multiply(b);
        System.Console.WriteLine("\nTranspose: ");
        sparse_matrix atranspose = a.transpose();
        atranspose.print();
    }
}
 
// This code is contributed by mits


Javascript




// JavaScript code to perform add,
// multiply and transpose on sparse matrices
class sparse_matrix {
 
    constructor(r, c)
    {
     
        // Maximum number of elements in matrix
        this.MAX = 100;
         
        // Array representation
        // of sparse matrix
        //[][0] represents row
        //[][1] represents col
        //[][2] represents value
        this.data = new Array(this.MAX);
        for (var i = 0; i < this.MAX; i++)
            this.data[i] = new Array(3);
 
        // dimensions of matrix
        this.row = r;
        this.col = c;
 
        // total number of elements in matrix
        this.len = 0;
    }
 
    // insert elements into sparse matrix
    insert(r, c, val)
    {
 
        // invalid entry
        if (r > this.row || c > this.col) {
            console.log("Wrong entry");
        }
 
        else {
 
            // insert row value
            this.data[this.len][0] = r;
 
            // insert col value
            this.data[this.len][1] = c;
 
            // insert element's value
            this.data[this.len][2] = val;
 
            // increment number of data in matrix
            this.len++;
        }
    }
 
    add(b)
    {
 
        // if matrices don't have same dimensions
        if (this.row != b.row || this.col != b.col) {
            console.log("Matrices can't be added");
        }
 
        else {
 
            let apos = 0, bpos = 0;
            let result
                = new sparse_matrix(this.row, this.col);
 
            while (apos < this.len && bpos < b.len) {
 
                // if b's row and col is smaller
                if (this.data[apos][0] > b.data[bpos][0]
                    || (this.data[apos][0]
                            == b.data[bpos][0]
                        && this.data[apos][1]
                               > b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(b.data[bpos][0],
                                  b.data[bpos][1],
                                  b.data[bpos][2]);
 
                    bpos++;
                }
 
                // if a's row and col is smaller
                else if (this.data[apos][0]
                             < b.data[bpos][0]
                         || (this.data[apos][0]
                                 == b.data[bpos][0]
                             && this.data[apos][1]
                                    < b.data[bpos][1]))
 
                {
 
                    // insert smaller value into result
                    result.insert(this.data[apos][0],
                                  this.data[apos][1],
                                  this.data[apos][2]);
 
                    apos++;
                }
 
                else {
 
                    // add the values as row and col is same
                    let addedval = this.data[apos][2]
                                   + b.data[bpos][2];
 
                    if (addedval != 0)
                        result.insert(this.data[apos][0],
                                      this.data[apos][1],
                                      addedval);
                    // then insert
                    apos++;
                    bpos++;
                }
            }
 
            // insert remaining elements
            while (apos < this.len)
                result.insert(this.data[apos][0],
                              this.data[apos][1],
                              this.data[apos++][2]);
 
            while (bpos < b.len)
                result.insert(b.data[bpos][0],
                              b.data[bpos][1],
                              b.data[bpos++][2]);
 
            // print result
            result.print();
        }
    }
 
    transpose()
    {
 
        // new matrix with inversed row X col
        let result = new sparse_matrix(this.col, this.row);
 
        // same number of elements
        result.len = this.len;
 
        // to count number of elements in each column
        let count = new Array(this.col + 1);
 
        // initialize all to 0
        for (var i = 1; i <= this.col; i++)
            count[i] = 0;
 
        for (var i = 0; i < this.len; i++)
            count[this.data[i][1]]++;
 
        let index = new Array(this.col + 1);
 
        // to count number of elements having col smaller
        // than particular i
 
        // as there is no col with value < 1
        index[1] = 0;
 
        // initialize rest of the indices
        for (var i = 2; i <= this.col; i++)
 
            index[i] = index[i - 1] + count[i - 1];
 
        for (var i = 0; i < this.len; i++) {
 
            // insert a data at rpos and increment its value
            var rpos = index[this.data[i][1]]++;
 
            // transpose row=col
            result.data[rpos][0] = this.data[i][1];
 
            // transpose col=row
            result.data[rpos][1] = this.data[i][0];
 
            // same value
            result.data[rpos][2] = this.data[i][2];
        }
 
        // the above method ensures
        // sorting of transpose matrix
        // according to row-col value
        return result;
    }
 
    multiply(b)
    {
 
        if (this.col != b.row) {
 
            // Invalid multiplication
            console.log("Can't multiply, "
                        + "Invalid dimensions");
 
            return;
        }
 
        // transpose b to compare row
        // and col values and to add them at the end
        b = b.transpose();
        let apos, bpos;
 
        // result matrix of dimension row X b.col
        // however b has been transposed, hence row X b.row
        let result = new sparse_matrix(this.row, b.row);
 
        // iterate over all elements of A
        for (apos = 0; apos < this.len;) {
 
            // current row of result matrix
            let r = this.data[apos][0];
 
            // iterate over all elements of B
            for (bpos = 0; bpos < b.len;) {
 
                // current column of result matrix
                // data[][0] used as b is transposed
                let c = b.data[bpos][0];
 
                // temporary pointers created to add all
                // multiplied values to obtain current
                // element of result matrix
                let tempa = apos;
                let tempb = bpos;
 
                let sum = 0;
 
                // iterate over all elements with
                // same row and col value
                // to calculate result[r]
                while (tempa < this.len
                       && this.data[tempa][0] == r
                       && tempb < b.len
                       && b.data[tempb][0] == c) {
 
                    if (this.data[tempa][1]
                        < b.data[tempb][1])
 
                        // skip a
                        tempa++;
 
                    else if (this.data[tempa][1]
                             > b.data[tempb][1])
 
                        // skip b
                        tempb++;
                    else
 
                        // same col, so multiply and
                        // increment
                        sum += this.data[tempa++][2]
                               * b.data[tempb++][2];
                }
 
                // insert sum obtained in result[r]
                // if its not equal to 0
                if (sum != 0)
                    result.insert(r, c, sum);
 
                while (bpos < b.len && b.data[bpos][0] == c)
 
                    // jump to next column
                    bpos++;
            }
 
            while (apos < this.len
                   && this.data[apos][0] == r)
 
                // jump to next row
                apos++;
        }
 
        result.print();
    }
 
    // printing matrix
    print()
    {
        console.log("Dimension: " + this.row + "x"
                    + this.col);
        console.log("Sparse Matrix: \nRow Column Value");
 
        for (var i = 0; i < this.len; i++) {
 
            console.log(this.data[i][0] + " "
                        + this.data[i][1] + " "
                        + this.data[i][2]);
        }
    }
};
 
// create two sparse matrices and insert values
let a = new sparse_matrix(4, 4);
let b = new sparse_matrix(4, 4);
 
a.insert(1, 2, 10);
a.insert(1, 4, 12);
a.insert(3, 3, 5);
a.insert(4, 1, 15);
a.insert(4, 2, 12);
b.insert(1, 3, 8);
b.insert(2, 4, 23);
b.insert(3, 3, 9);
b.insert(4, 1, 20);
b.insert(4, 2, 25);
 
// Output result
console.log("Addition: ");
a.add(b);
console.log("\nMultiplication: ");
a.multiply(b);
console.log("\nTranspose: ");
let atranspose = a.transpose();
atranspose.print();
 
// This code is contributed by phasing17


Output

Addition: 
Dimension: 4x4
Sparse Matrix: 
Row    Column    Value
1     2     10
1     3     8
1     4     12
2     4     23
3     3     14
4     1     35
4     2     37

Multiplication: 
Dimension: 4x4
Sparse Matrix: 
Row    Column    Value
1     1     240
1     2     300
1     4     230
3     3     45
4     3     120
4     4     276

Transpose: 
Dimension: 4x4
Sparse Matrix: 
Row    Column    Value
1     4     15
2     1     10
2     4     12
3     3     5
4     1     12

Worst case time complexity: Addition operation traverses the matrices linearly, hence, has a time complexity of O(n), where n is the number of non-zero elements in the larger matrix amongst the two. Transpose has a time complexity of O(n+m), where n is the number of columns and m is the number of non-zero elements in the matrix. Multiplication, however, has a time complexity of O(x*n + y*m), where (x, m) is number of columns and terms in the second matrix; and (y, n) is number of rows and terms in the first matrix.



Previous Article
Next Article

Similar Reads

Generate matrix from given Sparse Matrix using Linked List and reconstruct the Sparse Matrix
Given a sparse matrix mat[][] of dimensions N*M, the task is to construct and represent the original matrix using a Linked List and reconstruct the givensparse matrix. Examples: Input: mat[][] = {{0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 2, 0, 0}, {0, 3, 0, 0, 4}, {0, 0, 5, 0, 0}}Output:Linked list representation: (4, 2, 5) ? (3, 4, 4) ? (3, 1, 3) ?
15+ min read
Range sum query using Sparse Table
We have an array arr[]. We need to find the sum of all the elements in the range L and R where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Examples: Input : 3 7 2 5 8 9 query(0, 5) query(3, 5) query(2, 4) Output : 34 22 15Note : array is 0 based indexed and queries too. Since there are no updates/modificatio
8 min read
Sparse Search
Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string. Examples: Input : arr[] = {"for", "geeks", "", "", "", "", "ide", "practice", "", "", "", "quiz"} x = "geeks" Output : 1 Input : arr[] = {"for", "geeks", "", "", "", "", "ide", "practice", "", "", "", "quiz"}, x = "ds" Ou
7 min read
How to store a Sparse Vector efficiently?
A sparse vector is a vector that has a large number of zeros so it takes unwanted space to store these zeroes.The task is to store a given sparse vector efficiently without storing the zeros. Examples: Input: vector = { 2, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 5 }
6 min read
Range maximum query using Sparse Table
Given an array arr[], the task is to answer queries to find the maximum of all the elements in the index range arr[L...R].Examples: Input: arr[] = {6, 7, 4, 5, 1, 3}, q[][] = {{0, 5}, {3, 5}, {2, 4}} Output: 7 5 5 Input: arr[] = {3, 34, 1}, q[][] = {{1, 2}} Output: 34 Approach: A similar problem to answer range minimum queries has been discussed he
9 min read
What is meant by Sparse Array?
A sparse array or sparse matrix is an array in which most of the elements are zero. Characteristics of Sparse array:The sparse array is an array in which most of the elements have the same value(the default value is zero or null).Sparse matrices are those array that has the majority of their elements equal to zero.A sparse array is an array in whic
6 min read
Prove that Sparse Graph is NP-Complete
Prerequisite: NP-Completeness, NP Class, Sparse Graph, Independent Set Problem: Given graph G = (V, E) and two integers a and b. A set of a number of vertices of G such that there are at most b edges between them is known as the Sparse Subgraph of graph G. Explanation: Sparse Subgraph problem is defined as follows: Input: An undirected graph G = (V
4 min read
Find Next Sparse Number
A number is Sparse if there are no two adjacent 1s in its binary representation. For example 5 (binary representation: 101) is sparse, but 6 (binary representation: 110) is not sparse. Given a number x, find the smallest Sparse number which greater than or equal to x. Examples: Input: x = 6 Output: Next Sparse Number is 8 Input: x = 4 Output: Next
10 min read
Range Minimum Query (Square Root Decomposition and Sparse Table)
We have an array arr[0 . . . n-1]. We should be able to efficiently find the minimum value from index L (query start) to R (query end) where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Example: Input: arr[] = {7, 2, 3, 0, 5, 10, 3, 12, 18}; query[] = [0, 4], [4, 7], [7, 8]Output: Minimum of [0, 4] is 0 Minim
15+ min read
Sparse Set
How to do the following operations efficiently if there are large number of queries for them. InsertionDeletionSearchingClearing/Removing all the elements. One solution is to use a Self-Balancing Binary Search Tree like Red-Black Tree, AVL Tree, etc. Time complexity of this solution for insertion, deletion and searching is O(Log n).We can also use
15+ min read
LCA for general or n-ary trees (Sparse Matrix DP approach )
In previous posts, we have discussed how to calculate the Lowest Common Ancestor (LCA) for a binary tree and a binary search tree (this, this and this). Now let’s look at a method that can calculate LCA for any tree (not only for binary tree). We use Dynamic Programming with Sparse Matrix Approach in our method. This method is very handy and fast w
15+ min read
Sparse Matrix and its representations | Set 1 (Using Arrays and Linked Lists)
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix. Why to use Sparse Matrix instead of simple matrix ? Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store onl
15+ min read
Sparse Matrix and its representations | Set 2 (Using List of Lists and Dictionary of keys)
Prerequisite : Sparse Matrix and its representations Set 1 (Using Arrays and Linked Lists)In this post other two methods of sparse matrix representation are discussed. List of ListsDictionary List of Lists (LIL) One of the possible representation of sparse matrix is List of Lists (LIL). Where one list is used to represent the rows and each row cont
15+ min read
Check if a given matrix is sparse or not
A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix, Examples: Input : 1 0 3 0 0 4
5 min read
Sparse Matrix Representations | Set 3 ( CSR )
If most of the elements in the matrix are zero then the matrix is called a sparse matrix. It is wasteful to store the zero elements in the matrix since they do not affect the results of our computation. This is why we implement these matrices in more efficient representations than the standard 2D Array. Using more efficient representations we can c
11 min read
Sparse Table
We have briefly discussed sparse table in Range Minimum Query (Square Root Decomposition and Sparse Table)Sparse table concept is used for fast queries on a set of static data (elements do not change). It does preprocessing so that the queries can be answered efficiently. Example Problem 1 : Range Minimum Query We have an array arr[0 . . . n-1]. We
15+ min read
Sparse Graph
Graphs are fundamental structures in computer science and mathematics and it is used to model relationships between objects. Understanding the various types of graphs is very important for anyone working in fields like data analysis, networking, and algorithm design. One such type is the sparse graph. This article will provide an in-depth look at s
9 min read
Javascript Program to Check if a given matrix is sparse or not
A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix, Examples: Input : 1 0 3 0 0 4
2 min read
Check if a given number is sparse or not
Write a function to check if a given number is Sparse or not. A number is said to be a sparse number if in the binary representation of the number no two or more consecutive bits are set. Example: Input: x = 72Output: trueExplanation: Binary representation of 72 is 01001000. There are no two consecutive 1's in binary representation Input: x = 12Out
6 min read
Minimum operations required to Sort the Array using following operations
Given an array arr[] of size N. Make the array arr[] sorted in non-decreasing order in the minimum number of operations where you can choose any integer x and replace elements arr[i] == x equal to 0 for, 0 ? i ? N - 1. Examples: Input: n = 3, arr[] = {3, 3, 2}Output: 1Explanation: If you choose x = 3 then after one operation array will become {0, 0
10 min read
Minimum operations to reduce array elements to zero using prefix operations and updates
Given an array arr[] of length N. We can apply 2 types of operations on element of arr[]: Choose a prefix let say arr[0, i] (0 <= i < N) and decrement all elements of prefix by 1. This operation can only apply if all elements in the prefix > 0. Choose an element arr[i] (0 <= i < N) and update it to 0.The task is to output the minimum
8 min read
Program for subtraction of matrices
The below program subtracts of two square matrices of size 4*4, we can change N for a different dimension. Implementation: C/C++ Code // C++ program for subtraction of matrices #include <bits/stdc++.h> using namespace std; #define N 4 // This function subtracts B[][] from A[][], and stores // the result in C[][] void subtract(int A[][N], int
7 min read
Python List Equality | Program to check if two given matrices are identical
We are given two square matrices of same order. Check if two given matrices are identical. Examples: Input : A = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] B = [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] Output: Matrices are identical We have existing solution for this problem please refer C Program to check if two give
3 min read
Queries on number of Binary sub-matrices of Given size
Given a Binary Matrix of size N X M, the task is to answer Q queries of the following type; Query(a, b): Find the number of sub matrices of size a X a with each of its element consisting of the Binary Number b. We basically need to find submatrices of given sizes with all 1s or all 0s. Examples: Input : N = 5, M = 4 m[][] = { { 0, 0, 1, 1 }, { 0, 0
12 min read
Find the intersection of two Matrices
Given two matrix A[][] and B[][] of same order m*n. The task is to find the intersection of both matrix as C, where: Cij = Aij if Aij = BijC = "*", otherwise. Examples: Input : A[N][N] = {{2, 4, 6, 8}, {1, 3, 5, 7}, {8, 6, 4, 2}, {7, 5, 3, 1}}; B[N][N] = {{0, 4, 3, 8}, {1, 3, 5, 7}, {8, 3, 6, 2}, {4, 5, 3, 4}}; Output : * 4 * 8 1 3 5 7 8 * * 2 * 5
6 min read
Count of matrices (of different orders) with given number of elements
Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are: (1, 8), (2, 4), (4, 2), (8, 1).Examples: Input: N = 8 Output: (1,
5 min read
XOR of XORs of all sub-matrices
Given a 'N*N' matrix, the task is to find the XOR of XORs of all possible sub-matrices.Examples: Input :arr = {{3, 1}, {1, 3}} Output : 0 Explanation: All the elements lie in 4 submatrices each. 4 being even, there total contribution towards final answer becomes 0. Thus, ans = 0. Input : arr = {{6, 7, 13}, {8, 3, 4}, {9, 7, 6}}; Output : 4 A Simple
6 min read
Check if matrix can be converted to another matrix by transposing square sub-matrices
Given two N X M matrices of integers. In an operation, we can transpose any square sub-matrix in matrix1. The task is to check if matrix1 can be converted to matrix2 with the given operation.Examples: Input: matrix1[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, matrix2[][] = { {1, 4, 7}, {2, 5, 6}, {3, 8, 9}} Output: Yes Transpose the whole matrix and
10 min read
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only
Given two n * m matrices A[][] and B[][], the task is to make both the matrices strictly increasing (both rows and columns) only by swapping two elements in different matrices if they are located in the corresponding position i.e. A[i][j] can only be swapped with B[i][j]. If possible then print Yes otherwise, No.Examples: Input: A[][] = {{2, 10}, B
7 min read
Program to concatenate two given Matrices of same size
Given two matrices A and B of size M x N, the task is to concatenate them into one matrix.Concatenation of matrix: The process of appending elements of every row of the matrix one after the other is known as the Concatenation of matrix. Examples: Input: A[][] = {{1, 2}, {3, 4}}, B[][] = {{5, 6}, {7, 8}} Output: 1 2 5 6 3 4 7 8 Explanation: Elements
6 min read
Article Tags :
Practice Tags :