Matrix - Nine Submatrices

Matrix - Nine Submatrices 

PROBLEM STATEMENT :

The program must accept an integer matrix of size N*N as the input. The program must divide the given matrix into 9 submatrices based on the following conditions. 
- The program must divide the given matrix into four submatrices of equal size. 
- Then the program must divide the top-right submatrix horizontally into two submatrices of equal size. 
- Then the program must divide the bottom-left submatrix vertically into two submatrices of equal size. 
- Then the program must divide the bottom-right submatrix into four submatrices of equal size. 
Finally, the program must print the sum of integers in each submatrix as the output. 
Note: The value of N is always a multiple of 4. 

Boundary Condition(s): 
4 <= N <= 100 
0 <= Matrix element value <= 1000 

Input Format: 
The first line contains N. 
The next N lines, each contains N integers separated by a space. 

Output Format: 
The first line contains 9 integers representing the sum of integers in the 9 submatrices. 

Example Input/Output 1: 
Input: 
10 20 55 65 
40 30 92 82 
11 23 34 24 
21 43 74 94 

Output: 
100 120 174 32 66 34 24 74 94 

Explanation: 
The 1st submatrix (2*2) is given below. 
10 20 
40 30 
The 2nd submatrix (1*2) is given below. 
55 65 
The 3rd submatrix (1*2) is given below. 
92 82 
The 4th submatrix (2*1) is given below. 
11 
21 
The 5th submatrix (2*1) is given below. 
23 
43 
The 6th, 7th, 8th and 9th submatrices (1*1) are given below. 
34 24 74 94 

Example Input/Output 2: Input: 8 7 2 3 0 3 4 2 7 4 7 9 4 3 4 6 8 5 3 3 0 3 6 2 6 0 4 5 0 1 3 4 6 7 5 8 8 9 2 6 7 0 8 0 9 7 8 6 6 6 9 8 4 2 1 8 4 8 9 9 4 1 2 2 4 Output: 56 37 31 52 50 26 25 6 18


SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int n,i,j,res[9]={0}; scanf("%d",&n); int a[n][n]; for(i=0;i<n;++i) { for(j=0;j<n;++j) { scanf("%d ",&a[i][j]); } } for(i=0;i<n;++i) { for(j=0;j<n;++j) { if(i>=0&&i<n/2&&j>=0&&j<n/2) res[0]+=a[i][j]; else if(i>=0&&i<n/4&&j>=n/2&&j<n) res[1]+=a[i][j]; else if(i>=n/4&&i<n/2&&j>=n/2&&j<n) res[2]+=a[i][j]; else if(i>=n/2&&i<n&&j>=0&&j<n/4) res[3]+=a[i][j]; else if(i>=n/2&&i<n&&j>=n/4&&j<n/2) res[4]+=a[i][j]; else if(i>=n/2&&i<(3*n/4)&&j>=n/2&&j<(3*n/4)) res[5]+=a[i][j]; else if(i>=n/2&&i<(3*n/4)&&j>=(3*n/4)&&j<n) res[6]+=a[i][j]; else if(i>=(3*n/4)&&i<n&&j>=n/2&&j<(3*n/4)) res[7]+=a[i][j]; else res[8]+=a[i][j]; } } for(i=0;i<9;++i) printf("%d ",res[i]); }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int n,i,j,res[9]={0}; cin>>n; int a[n][n]; for(i=0;i<n;++i) { for(j=0;j<n;++j) { cin>>a[i][j]; } } for(i=0;i<n;++i) { for(j=0;j<n;++j) { if(i>=0&&i<n/2&&j>=0&&j<n/2) res[0]+=a[i][j]; else if(i>=0&&i<n/4&&j>=n/2&&j<n) res[1]+=a[i][j]; else if(i>=n/4&&i<n/2&&j>=n/2&&j<n) res[2]+=a[i][j]; else if(i>=n/2&&i<n&&j>=0&&j<n/4) res[3]+=a[i][j]; else if(i>=n/2&&i<n&&j>=n/4&&j<n/2) res[4]+=a[i][j]; else if(i>=n/2&&i<(3*n/4)&&j>=n/2&&j<(3*n/4)) res[5]+=a[i][j]; else if(i>=n/2&&i<(3*n/4)&&j>=(3*n/4)&&j<n) res[6]+=a[i][j]; else if(i>=(3*n/4)&&i<n&&j>=n/2&&j<(3*n/4)) res[7]+=a[i][j]; else res[8]+=a[i][j]; } } for(i=0;i<9;++i) cout<<res[i]<<" "; }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),i,j; int[][] a=new int[n][n]; int[] res=new int[9]; for(i=0;i<n;++i) { for(j=0;j<n;++j) { a[i][j]=sc.nextInt(); } } for(i=0;i<n;++i) { for(j=0;j<n;++j) { if(i>=0&&i<n/2&&j>=0&&j<n/2) res[0]+=a[i][j]; else if(i>=0&&i<n/4&&j>=n/2&&j<n) res[1]+=a[i][j]; else if(i>=n/4&&i<n/2&&j>=n/2&&j<n) res[2]+=a[i][j]; else if(i>=n/2&&i<n&&j>=0&&j<n/4) res[3]+=a[i][j]; else if(i>=n/2&&i<n&&j>=n/4&&j<n/2) res[4]+=a[i][j]; else if(i>=n/4&&i<(3*n/4)&&j>=n/4&&j<(3*n/4)) res[5]+=a[i][j]; else if(i>=n/4&&i<(3*n/4)&&j>=(3*n/4)&&j<n) res[6]+=a[i][j]; else if(i>=(3*n/4)&&i<n&&j>=n/2&&j<(3*n/4)) res[7]+=a[i][j]; else res[8]+=a[i][j]; } } for(i=0;i<9;++i) System.out.print(res[i]+" "); } }



PYTHON

       

n=int(input()) l=[list(map(int,input().split())) for i in range(n)] res=[0]*9 for i in range(n): for j in range(n): if i>=0 and i<n//2 and j>=0 and j<n//2: res[0]+=l[i][j] elif i>=0 and i<n//4 and j>=n//2 and j<n: res[1]+=l[i][j] elif i>=n//4 and i<n//2 and j>=n//2 and j<n: res[2]+=l[i][j] elif i>=n//2 and i<n and j>=0 and j<n//4: res[3]+=l[i][j] elif i>=n//2 and i<n and j>=n//4 and j<n//2: res[4]+=l[i][j] elif i>=n//2 and i<(3*n//4) and j>=n//2 and j<(3*n//4): res[5]+=l[i][j] elif i>=n//2 and i<(3*n//4) and j>=(3*n//4) and j<n: res[6]+=l[i][j] elif i>=(3*n//4) and i<n and j>=n//2 and j<(3*n//4): res[7]+=l[i][j] elif i>=(3*n//4) and i<n and j>=(3*n//4) and j<n: res[8]+=l[i][j] print(*res)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right