Header Ads Widget

Matrix sum | Infytq solution

 Problem:-  Given a row/column count and a matrix, your job is to find those possible 2*2 matrix where each should follow the given rule :

-> Each element of matrix should be divisible by sum of its digits.

Input : 

First Input : Row count, Column Count

Second Input : Matrix

Output : 

2*2 matrices satisfying the rule.

Sample Testcases :


I/P 1:

4 3

40 42 2

30 24 27

180 190 40

11 121 13

O/P 1:

40 42

30 24

30 24

180 190

24 27

190 40

Code:-

r, c = list(map(int, input().split()))
mat = []
for i in range(r):
l = list(map(int, input().split()))
mat.append(l)

for i in range(r - 1):
s = 0
for j in range(c - 1):
n = mat[i][j]
s = 0
while (n != 0):
s += (n % 10)
n //= 10
if (mat[i][j] % s != 0):
break
n = mat[i + 1][j]
s = 0
while (n != 0):
s += (n % 10)
n //= 10
if (mat[i + 1][j] % s != 0):
break
n = mat[i][j + 1]
s = 0
while (n != 0):
s += (n % 10)
n //= 10
if (mat[i][j + 1] % s != 0):
break
n = mat[i + 1][j + 1]
s = 0
while (n != 0):
s += (n % 10)
n //= 10
if (mat[i + 1][j + 1] % s != 0):
break
print(mat[i][j], mat[i][j + 1])
print(mat[i + 1][j], mat[i + 1][j + 1])

Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments