Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Online

Learning
#InfyTQ Preparation
(For 2022 Batch) Previous Year
Questions Series + Practice

For InfyTQ & HackwithInfy


Updates Join Telegram Group:-
https://telegram.me/infytq_2022

For All Updates On YouTube


Check Channel
Link:-
https://www.youtube.com/c/OnlineLea
rnin gNewJobUpdates/featured
Online
Learning
Q2.

Read ‘m’ and ‘n’.


Take m*n matrix.

If any number is consecutive 3 times either in row


,column ,diagonals print that number , if there are
multiple numbers then print min of those numbers.

Ex: m=6 ,n=7,take 6*7 matrix

2345624
2347676
2355552
2311213
1111903
2311512

Output:- 1

Solutions:-

(I). In Python:-
Online
Learning
row,column=map(int,input().split(" "))
matrix_digit=[]
matrix=[]

for r in range(row):
row_numbers=list(map(int,input().split()))
matrix.append(row_numbers)

for r in range(0,row):
for c in range(0,column):
if(c < column-3): #consecutive numbers in all rows in
matrix

if(matrix[r][c]==matrix[r][c+1]==matrix[r][c+2]==matrix[r][c
+3]):
matrix_digit.append(matrix[r][c])

if(r < row-3): #consecutive numbers in all columns in matrix

if(matrix[r][c]==matrix[r+1][c]==matrix[r+2][c]==matrix[r+3]
[c]):
matrix_digit.append(matrix[r][c])

if(c < column-3 and r >= 3): #consecutive numbers in all left to
right diagonals in matrix
if(matrix[r][c]==matrix[r-1][c+1]==matrix[r-
2][c+2]==matrix[r-3][c+3]):
matrix_digit.append(matrix[r][c])

if(c >= 3 and r >= 3): #consecutive numbers in all left to right
Online
Learning
diagonals in matrix
if(matrix[r][c]==matrix[r-1][c-1]==matrix[r-2][c-
2]==matrix[r-3][c-3]):
matrix_digit.append(matrix[r][c])

if(len(matrix_digit)==0):
print("-1")
else:
print(min(matrix_digit))

Output:-
Online
Learning
II. In Java: -

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;public class Question3 {
public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str[]=br.readLine().split(" ");//first line will row and column value separated with space
int row=Integer.parseInt(str[0]);
int column=Integer.parseInt(str[1]);
int matrix[][]=new int[row][column];

for(int i=0;i<row;i++)//
{
String rowData[]=br.readLine().split(" ");
for(int j=0;j<column;j++)
{
matrix[i][j]=Integer.parseInt(rowData[j]);
}
}

ArrayList<Integer>list=new ArrayList<>();
for (int r=0;r<row;r++)
{
for(int c=0;c<column;c++){
if(c < column-3) //

if(matrix[r][c]==matrix[r][c+1] && matrix[r][c+2]==matrix[r][c+3] &&


matrix[r][c+1]==matrix[r][c+2])
list.add(matrix[r][c]);
}
if(r < row-3) // #
{
if(matrix[r][c]==matrix[r+1][c] && matrix[r+2][c]==matrix[r+3][c] &&
matrix[r+1][c]==matrix[r+2][c])
list.add(matrix[r][c]);
} if(c < column-3 && r >= 3) //
{
if(matrix[r][c]==matrix[r-1][c+1] && matrix[r-2][c+2]==matrix[r-3][c+3] && matrix[r-
1][c+1]==matrix[r-2][c+2])
list.add(matrix[r][c]);
}

if(c >= 3 && r >= 3) //


{
if(matrix[r][c]==matrix[r-1][c-1] && matrix[r-2][c-2]==matrix[r-3][c-3] && matrix[r-1][c-
1]==matrix[r-2][c-2])
list.add(matrix[r][c]);
Online
Learning
}
}
}
if(list.size()==0)
System.out.println("-1");
else
System.out.println(Collections.min(list));
}

You might also like