Join Regular Classroom : Visit ClassroomTech

Matrix and Digit Sum – Infytq 2019 Solve

Problem: Find all 2 x 2 matrix in which each of the element of that 2 x 2 must be divisible by the sum of their digits, and print them.

Input format: The first line contains an integer denoting the number of rows.

Sample Input:

4
42 54 2
30 24 27
180 190 40
11 121 13

Sample Output:

42 54
30 24
54 2
24 27
30 24
180 190
24 27
190 40

Explanation:
So, we see that first 2 x 2 matrix i.e.
42 54
30 24
Satisfies the given condition, 42 is divisible by 6 (4+2), 54 is divisible by 9 (5+4), 30 is divisible by 3 (3+0) and 24 is divisible by 6 (2+4).

More such matrixes are,
54 2
24 27

30 24
180 190

24 27
190 40

Solution: We strongly recommend you to try the problem first before moving to the solution.

Python

# Code to understand the Matrix and its digit sum problem in Python
# www.codewindow.in

def divisible(n):
    s = sum(list(map(int, str(n))))
    if n%s==0:
        return True
    else:
        return False
    
row = int(input())
matrix = []
for i in range(row):
    matrix.append(list(map(int, input().split())))
    
column=len(matrix[0])
for r in range(row-1):
    
    for c in range(column-1):
        if(divisible(matrix[r][c]) and divisible(matrix[r][c+1]) and divisible(matrix[r+1][c]) and divisible(matrix[r+1][c+1])):
            print(matrix[r][c], matrix[r][c+1])
            print(matrix[r+1][c], matrix[r+1][c+1])

Input:

4
42 54 2
30 24 27
180 190 40
11 121 13

Output:

42 54
30 24
54 2
24 27
30 24
180 190
24 27
190 40

Follow Us

You Missed