Reduce given Matrix by size 1 using sum of all 2×2 submatrices
Last Updated :
13 Mar, 2022
Given a square matrix M[][] of size N*N, the task is to reduce that matrix into a matrix of size (N-1) * (N-1) using the following operation:
- Take all the 2×2 submatrix from N*N matrix,
- Insert sum of each submatrix to the resultant matrix L[ ][ ] of size N-1 * N-1 at the corresponding position.
Examples:
Input: M[][] = {{ 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 }}
Output: L[][] = {{12, 16, 20},
{24, 28, 32},
{36, 40, 44}}
Explanation:
Input: M[][] = {{ 3, 6, 3},
{ 4, 7, 6},
{ 2, 1, 9}}
Output: L[][] = {{20, 22},
{14, 23}}
Approach: The task can be solved by simulating the process of the defined operation. Follow the below steps to solve the problem:
- Declare empty array l[] to store element of reduced matrix
- Declare c = s = 0, s for taking sum and c for changing column and p[ ] for storing sum after every iteration.
- Now s is, s = M[ i ][ c ] + M[ i ][ c + 1 ] + M[ i + 1 ][ c ] + M[ i+1 ][ c+1 ]
- It will store 4 elements, like element at index [00, 01, 10, 11], then [01, 02, 11, 12] similarly from row 2 and 3
- Store the first sum in array p[] and increment c and again assign s=0.
- Repeat step 3, hence we will get array p [ ].
- Store p in matrix l [ ] and repeat step 2.
Below is the implementation of the approach:
C++14
#include <bits/stdc++.h>
using namespace std;
vector<vector< int > > checkMat(vector<vector< int > >& M,
int N)
{
vector<vector< int > > l;
for ( int i = 0; i < N - 1; i++) {
int c = 0, s = 0;
vector< int > p;
for ( int j = 0; j < N - 1; j++) {
s += M[i][j] + M[i][j+1];
s += M[i + 1][j] + M[i + 1][j+1];
p.push_back(s);
s = 0;
c++;
}
l.push_back(p);
}
return l;
}
int main()
{
vector<vector< int > > M = { { 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 } };
int N = M.size();
vector<vector< int > > res = checkMat(M, N);
for ( int i = 0; i < res.size(); i++) {
for ( int j = 0; j < res[i].size(); j++) {
cout << res[i][j] << " " ;
}
cout << "\n" ;
}
}
|
Java
import java.util.*;
class GFG
{
static ArrayList<ArrayList<Integer> >
checkMat( int [][] M, int N)
{
ArrayList<ArrayList<Integer> > l
= new ArrayList<ArrayList<Integer> >( 2 );
for ( int i = 0 ; i < N - 1 ; i++) {
ArrayList<Integer> p = new ArrayList<Integer>();
int c = 0 , s = 0 ;
for ( int j = 0 ; j < N - 1 ; j++) {
s += M[i][j] + M[i][j+ 1 ];
s += M[i + 1 ][j] + M[i + 1 ][j+ 1 ];
p.add(s);
s = 0 ;
c++;
}
l.add(p);
}
return l;
}
public static void main(String[] args)
{
int [][] M = { { 1 , 2 , 3 , 4 },
{ 4 , 5 , 6 , 7 },
{ 7 , 8 , 9 , 10 },
{ 10 , 11 , 12 , 13 } };
int N = M.length;
ArrayList<ArrayList<Integer> > res = checkMat(M, N);
for ( int i = 0 ; i < res.size(); i++) {
for ( int j = 0 ; j < res.get(i).size(); j++) {
System.out.print(res.get(i).get(j) + " " );
}
System.out.println();
}
}
}
|
Python3
def checkMat(M, N):
l = []
for i in range (N - 1 ):
c = s = 0
p = []
for j in range (N - 1 ):
s + = M[i][j] + M[i][j + 1 ]
s + = M[i + 1 ][j] + M[i + 1 ][j + 1 ]
p.append(s)
s = 0
c + = 1
l.append(p)
return l
if __name__ = = "__main__" :
M = [[ 1 , 2 , 3 , 4 ],
[ 4 , 5 , 6 , 7 ],
[ 7 , 8 , 9 , 10 ],
[ 10 , 11 , 12 , 13 ]]
N = len (M)
res = checkMat(M, N)
for i in res:
print ( * i)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static List<List< int > >
checkMat( int [,] M, int N)
{
List<List< int > > l
= new List<List< int > >(2);
for ( int i = 0; i < N - 1; i++) {
List< int > p = new List< int >();
int c = 0, s = 0;
for ( int j = 0; j < N - 1; j++) {
s += M[i,c] + M[i,c + 1];
s += M[i + 1,c] + M[i + 1,c + 1];
p.Add(s);
s = 0;
c++;
}
l.Add(p);
}
return l;
}
public static void Main(String[] args)
{
int [,] M = { { 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 } };
int N = M.GetLength(0);
List<List< int > > res = checkMat(M, N);
for ( int i = 0; i < res.Count; i++) {
for ( int j = 0; j < res[i].Count; j++) {
Console.Write(res[i][j] + " " );
}
Console.WriteLine();
}
}
}
|
Javascript
<script>
const checkMat = (M, N) => {
let l = [];
for (let i = 0; i < N - 1; ++i) {
let c = s = 0;
let p = [];
for (let j = 0; j < N - 1; ++j) {
s += M[i][j] + M[i][j+1];
s += M[i + 1][j] + M[i + 1][j+1];
p.push(s);
s = 0;
c += 1;
}
l.push(p);
}
return l;
}
let M = [[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]];
let N = M.length;
let res = checkMat(M, N);
for (let i in res) {
for (let j in res[i]) {
document.write(`${res[i][j]} `);
}
document.write( "<br/>" );
}
</script>
|
Output
12 16 20
24 28 32
36 40 44
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...