You are here : InfyTQ Archive >> InfyTQ Coding Problems
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
#My solution in Python
#NOTE:- IN GIVEN EXAMPLE THERE IS AN ERROR EITHER IN OUTPUT(AS OUTPUT ALSO INCLUDE 42 2 24 27) OR INPUT(IN PLACE OF 2 THERE IS SOMETHING ELSE)
#CORRECT ME IF I AM WRONG
row,col=map(int,input().split())
matrix=[]
for _ in range(row):
matrix.append(input().split())
for i in range(row-1):
for j in range(col-1):
a= matrix[i][j]
b= matrix[i][j+1]
c= matrix[i+1][j]
d= matrix[i+1][j+1]
if int(a)%sum(map(int,list(a)))==0 and int(b)%sum(map(int,list(b)))==0 and int(c)%sum(map(int,list(c)))==0 and int(d)%sum(map(int,list(d)))==0:
print(a, b,)
print(c, d)
is this code is executed
rows, cols = map(int, input().split())
mat = []
for i in range(rows):
mat.append(list(map(int, input().split())))
for i in range(rows-1):
for j in range(cols-1):
a = mat[i][j]
b = mat[i][j+1]
c = mat[i+1][j]
d = mat[i+1][j+1]
all_occ = [a, b, c, d]
if len(list(filter(lambda item: item % sum(map(int, list(str(item)))) == 0, all_occ))) == 4:
print(a, b)
print(c, d)
row, col = map(int, input().split())
arr = []
for i in range(0, row):
arr.append(list(map(int, input().split())))
for i in range(0, row-1):
for j in range(0, col-1):
count = 0
for k in range(i,i+2):
for l in range(j,j+2):
sdl = sum([int(z) for z in str(arr[k][l])])
#print(sdl)
if arr[k][l] % sdl == 0:
count += 1
if count == 4:
print(arr[i][j],arr[i][j+1])
print(arr[i+1][j],arr[i+1][j+1])
def is_divisible(n):
s = sum(list(map(int,str(n))))
if n%s==0:
return True
else:
return False
row, col = map(int, input().split())
matrix = []
for i in range(row):
matrix.append(list(map(int,input().split())))
col=len(matrix[0])
for r in range(row-1):#3
for c in range(col-1):#2
if(is_divisible(matrix[r][c]) and is_divisible(matrix[r][c+1]) and is_divisible(matrix[r+1][c]) and is_divisible(matrix[r+1][c+1])):
print(matrix[r][c],matrix[r][c+1])
print(matrix[r+1][c],matrix[r+1][c+1])
r,c = list(map(int,input().split()))
def condition(num):
sum = 0
a = num
while num:
sum = sum + num%10
num = num // 10
return a%sum == 0
mat = []
output = []
for _ in range(r):
mat.append(list(map(int, input().split())))
for i in range(r-1):
for j in range(c-1):
if condition(mat[i][j]) and condition(mat[i][j+1]) and condition(mat[i+1][j]) and condition(mat[i+1][j+1]):
output.append([ mat[i][j], mat[i][j+1] ])
output.append([ mat[i+1][j], mat[i+1][j+1] ])
print(output)
import java.io.*;
import java.util.*;
import java.util.Collections;
public class Matrix {
public static boolean checkSum(int a,int b,int c,int d){
int count=0;
int[] tt={a,b,c,d};
try{
for(int x:tt){
int ele=x;
int sum=0;
while(x>0){
sum=sum+(x%10);
x=x/10;
}
if(ele%sum==0)
count++;
}
}catch(ArithmeticException e){
// e.printStackTrace();
return false;
}
if(count==4) return true;
else return false;
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().split(” “);
int r = Integer.parseInt(str[0]);
int c = Integer.parseInt(str[1]);
int mat[][] = new int[r][c];
// int two[][] = new int[r][c];
for(int i = 0; i < r; i++) {
String in[] = br.readLine().split(" ");
for(int j = 0; j < c; j++) {
mat[i][j] = Integer.parseInt(in[j]);}}
ArrayList al = new ArrayList();
for(int i = 0; i < mat.length-1; i++) {
for(int j = 0; j 0){
System.out.print(al.get(0)+” “);
al.remove(0);
}
System.out.println(“———–“);
}
}}
import java.util.*;
class abc
{
public int getSumDigit(int num)
{
int s=0;
while(num!=0)
{
int rem = num %10;
s = rem + s;
num = num / 10;
}
return s;
}
}
public class coding15
{
public static void main(String[] args)
{
Scanner sc=new Scanner (System.in);
abc abc=new abc();
System.out.println(“Enter number of rows : “);
int m= sc.nextInt();
System.out.println(“Enter number of columns : “);
int n= sc.nextInt();
int[][] a= new int[m][n];
for(int k=0;k<m;k++)
{
for(int l=0;l<n;l++)
{
a[k][l]= sc.nextInt();
}
}
for (int i=0;i<m-1;i++)
{
for (int j=0;j<n-1;j++)
{
int sum= a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1];
if (a[i][j]%abc.getSumDigit(a[i][j])==0 && a[i][j+1]%abc.getSumDigit(a[i][j+1])==0
&& a[i+1][j]%abc.getSumDigit(a[i+1][j])==0 && a[i+1][j+1]%abc.getSumDigit(a[i+1][j+1])==0)
{
System.out.println(a[i][j] +" "+a[i][j+1]);
System.out.println(a[i+1][j]+" "+a[i+1][j+1]);
}
}
}
}
}