You are here : InfyTQ Archive >> InfyTQ Coding Problems
Given m*n matrix where m defines number of rows and n defines number of columns. Your job is to check whether a number is consecutive for 3 times either in row, column, or diagonal. If there are more than one such numbers then print the minimum one.
Note : n = m+1
Input :
First Input : m, displaying number of rows
Second Input : m*n matrix
Output :
Integer
Sample Testcases :
I/P 1:
2 3 4 5 6 2 4 3
2 3 4 7 6 7 6 2
2 3 5 5 5 5 2 5
2 3 1 1 2 1 3 6
1 1 1 1 9 0 3 5
2 3 1 1 5 1 2 7
O/P 1 : 1
Note : Make combinations with 4 integers only.
Solution :
r = int(input())
mat = []
for i in range(r):
mat.append(list(map(int,input().split())))
c = r+1
res=[]
for i in range(r):
for j in range(c-2):
if mat[i][j] == mat[i][j+1] == mat[i][j+2]:
res.append(mat[i][j])
for i in range(r-2):
for j in range(c):
if mat[i][j] == mat[i+1][j] == mat[i+2][j]:
res.append(mat[i][j])
for i in range(r-2):
for j in range(c-2):
if mat[i][j] == mat[i+1][j+1] == mat[i+2][j+2]:
res.append(mat[i][j])
print(res)
The solution you mentioned for the given problem statement is incorrect.
This Python solution was provided by our user. If you think, your solution is better than this, then please go ahead and post your solution here.
n=int(input())
m=n+1
mat=[]
ml=[]
for i in range (n):
row=list(map(int,input().split()))
mat.append(row)
for i in range(n):
for j in range(m):
if(j< (m-4)):
if( mat[i][j]==mat[i][j+1]==mat[i][j+2]==mat[i][j+3] ):
ml.append(mat[i][j])
if(i<= n-4):
if(mat[i][j]==mat[i+1][j]==mat[i+2][j]==mat[i+3][j] ):
ml.append(mat[i][j])
print(min(ml))
pls give the correct code
n=int(input())
m=n+1
mat=[]
ml=[]
for i in range (n):
row=list(map(int,input().split()))
mat.append(row)
for i in range(n):
for j in range(m):
if(j< (m-4)):
if( mat[i][j]==mat[i][j+1]==mat[i][j+2]==mat[i][j+3] ):
ml.append(mat[i][j])
if(i<= n-4):
if(mat[i][j]==mat[i+1][j]==mat[i+2][j]==mat[i+3][j] ):
ml.append(mat[i][j])
print(min(ml))