VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-subtraction-matices/

⇱ Program for subtraction of matrices - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for subtraction of matrices

Last Updated : 1 Nov, 2024

Given two m x n matrices m1 and m2, the task is to subtract m2 from m1 and return res.

Input: m1 = {{1, 2},
{3, 4}},
m2 = {{4, 3},
{2, 1}}
Output: {{-3, -1},
{1, 3}}

Input: m1 = {{3, 3, 3},
{3, 3, 3}},
m1 = {{2, 2, 2},
{1, 1, 1}},
Output: {{1, 1, 1},
{2, 2, 2}},

We traverse both matrices element by element and subtract m2[i][j] from m1[i][j].


Output
Result matrix is:
0 1 2 
3 4 5 

Time complexity: O(n2)

Comment
Article Tags: