Submatrices - Diagonal Elements Sum :

Submatrices - Diagonal Elements Sum : 

PROBLEM STATEMENT : 

The program must accept an integer matrix M of size R*C and an integer N as the input. The program must print the sum of diagonal elements of all the N*N non-overlapping submatrices in M as the output. 

Note: The value of N is always greater than 1 and less than or equal to the minimum value between R and C. 

Boundary Condition(s): 
2 <= R, C <= 50 
0 <= Matrix element value <= 1000 

Input Format: 
The first line contains R and C separated by a space. 
The next R lines, each contains C integer values separated by a space. 
The (R+2)th line contains N. 

Output Format: 
The first line contains the integer values representing the sum of diagonal elements of all N*N non-overlapping submatrices in M. 

Example Input/Output 1: 
Input: 
5 6 
6 3 6 3 2 1
4 5 7 1 3 8
6 9 8 4 3 2 
4 1 8 8 9 1 
8 1 5 7 9 0 

Output: 
31 13 

Explanation: 
There are 2 non-overlapping submatrices of size 3*3 in the given 5*6 matrix. 
In the first submatrix, the sum of diagonal elements is 31 (6+5+8+6+6). 
In the second submatrix, the sum of diagonal elements is 13 (3+3+2+1+4). 
Hence the output is 31 13 

Example Input/Output 2: 
Input: 
6 6 
6 3 6 3 2 1 
4 5 7 1 3 8 
6 9 8 4 3 2 
4 1 8 8 9 1 
8 1 5 7 9 0 
1 0 6 9 2 1 

Output: 
31 13 20 28


SOLUTIONS :

C (Programming Language)

       

#include<stdio.h> #include<stdlib.h> int main() { int r,c,i,j,i1,j1,n; scanf("%d %d\n",&r,&c); int m[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { scanf("%d ",&m[i][j]); } } scanf("%d",&n); for(i=0;i<r;i+=n) { for(j=0;j<c;j+=n) { if(i+n>r||j+n>c) continue; int s=0; for(i1=i;i1<i+n;i1++) { for(j1=j;j1<j+n;j1++) { if(i1%n==j1%n||(i1+j1)%n==n-1) { s+=m[i1][j1]; } } } printf("%d ",s); } } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int r,c,i,j,i1,j1,n; cin>>r>>c; int m[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { cin>>m[i][j]; } } cin>>n; for(i=0;i<r;i+=n) { for(j=0;j<c;j+=n) { int s=0; if(i+n>r||j+n>c) continue; for(i1=i;i1<i+n;i1++) { for(j1=j;j1<j+n;j1++) { if(i1%n==j1%n||(i1+j1)%n==n-1) { s+=m[i1][j1]; } } } cout<<s<<" "; } } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int r=sc.nextInt(),c=sc.nextInt(),i,j,i1,j1,n; int[][] m=new int[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { m[i][j]=sc.nextInt(); } } n=sc.nextInt(); for(i=0;i<r;i+=n) { for(j=0;j<c;j+=n) { if(i+n>r||j+n>c) continue; int s=0; for(i1=i;i1<i+n;i1++) { for(j1=j;j1<j+n;j1++) { if(i1%n==j1%n||(i1+j1)%n==n-1) { s+=m[i1][j1]; } } } System.out.print(s+" "); } } } }



PYTHON

       

r,c=map(int,input().split()) m=[list(map(int,input().split())) for i in range(r)] n=int(input()) for i in range(0,r,n): for j in range(0,c,n): if i+n<=r and j+n<=c: s=sum([m[i1][j1] for i1 in range(i,i+n) for j1 in range(j,j+n) if i1%n==j1%n or (i1+j1)%n==n-1]) print(s,end=' ')



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