The program must accept a matrix of size N*N containing only digits as the input. The value of N is always a power of two. The program must reduce the N*N matrix to 1*1 matrix based on the following conditions.
– The program must replace each 2*2 submatrix with the sum of the elements in the submatrix. Then the program must print the reduced matrix.
– Then the program must repeat above the process till the size of the matrix becomes 1*1.
Boundary Condition(s):
2 <= N <= 64
Input Format:
The first line contains N.
The next N lines, each contains N digits separated by a space.
Output Format:
The lines contain the reduced matrices.
Example Input/Output 1:
Input:
8
1 3 8 2 7 1 3 7
6 8 4 3 9 8 0 0
2 9 3 3 9 0 7 1
4 8 0 5 2 9 6 1
8 8 7 1 5 6 3 2
5 0 4 0 7 3 1 1
4 7 5 6 3 6 4 6
0 3 6 6 7 7 3 0
Output:
18 17 25 10
23 11 20 15
21 12 21 7
14 23 23 13
69 70
70 64
273
Explanation:
Here N = 8.
8*8 -> 4*4 -> 2*2 -> 1*1.
The 4*4 reduced matrix is given below.
18 17 25 10
23 11 20 15
21 12 21 7
14 23 23 13
The 2*2 reduced matrix is given below.
69 70
70 64
The 1*1 reduced matrix is given below.
273
Example Input/Output 2:
Input:
4
3 2 4 5
7 6 8 6
7 6 0 3
2 9 0 8
Output:
18 23
24 11
76
N = int(input())
L = [list(map(int, input().split())) for i in range(N)]
while N != 1:
NN = N // 2
NL = [[0 for i in range(NN)] for j in range(NN)]
for i in range(N):
for j in range(N):
NL[i//2][j//2] += L[i][j]
[print(*i) for i in NL]
L = NL.copy()
N = NN
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int mat[n][n],i,j,k,l;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d ",&mat[i][j]);
}
}
while(n>0){
int x=0,temp[n*n];
for(i=0;i<n;i+=2){
for(j=0;j<n;j+=2){
int sum=0;
for(k=i;k<i+2;k++){
for(l=j;l<j+2;l++){
sum+=mat[k][l];
}
}
temp[x++]=sum;
}
}
int y=0;
n/=2;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mat[i][j]=temp[y++];
printf("%d ",mat[i][j]);
}
printf("n");
}
}
}
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] mat = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
mat[i][j]=sc.nextInt();
}
}
sc.close();
int[][] arr = new int[n/2][n/2];
while(true){
int i1=0,j1=0;
for(int i=0;i<n;i+=2){
for(int j=0;j<n;j+=2){
arr[i1][j1]=mat[i][j]+mat[i][j+1]+mat[i+1][j]+mat[i+1][j+1];
j1++;
}
i1++;
j1=0;
}
print(arr);
if(arr.length==1 && arr[0].length==1){
break;
}else{
n/=2;
mat = new int[n][n];
copy(mat,arr);
arr = new int[n/2][n/2];
}
}
}
static void copy(int[][] m, int[][] a){
for(int i=0;i<m.length;i++){
for(int j=0;j<m[i].length;j++){
m[i][j]=a[i][j];
}
}
}
static void print(int[][] arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv){
int n;
cin>>n;
vector<vector<int>> arr(n,vector<int>(n));
for(int i = 0 ;i<n;i++)for(int j = 0 ;j<n;j++)
cin>>arr[i][j];
while(n>1){
int m= n/2;
vector<vector<int>> temp(m,vector<int>(m));
for(int i = 0 ;i<n;i++){
for(int j = 0;j<n;j++){
temp[i/2][j/2]+=arr[i][j];
}
}
swap(temp,arr);
for(int i = 0 ;i<m;i++){
for(int j = 0 ;j<m;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
n= m;
}
}