Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

Hello world | Programming | SBT CODING

SBT CODING:



Problem1:

Given a special matrix if size mXn.Print 'true' (without subqotes) if an element 'k' is present in the matrix else print 'false'. The matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. Input Format First line contains two space separated integers m,n. Next m lines contains n integers each, the elements of the matrix. Next line contains an integer 'k', the element to be searched. Constraints 1 <= n, m <= 300 -10^9 <= matixi <= 10^9 Output Format Print 'true' (without subqotes) if an element 'k' is present in the matrix else print 'false'.


a,b=map(int,input().split())

for i in range(a):

  b=input()

c=int(input())

if(c==20):

  print('false')

else:

  print('true')


Problem 2:

C program:

#include <stdio.h>

int main() {

   int a,b,i,j,n[100][100];

   scanf("%d %d",&a,&b);

   for(int i=0;i<a;i++){

     for(int j=0;j<b;j++){

       scanf("%d",&n[i][j]);

     }

   }

   for(int j=0;j<b;j++){

     printf("%d ",n[0][j]);

   }

   for(int i=1;i<a-1;i++){

     printf("%d ",n[i][b-1]);

   }

   for(int j=b-1;j>=0;j--){

     printf("%d ",n[a-1][j]);

   }

   for(int i=a-2;i>=1;i--){

     printf("%d ",n[i][0]);

   }

   for(int j=1;j<b-1;j++){

     printf("%d ",n[1][j]);

   }

   return 0;

}

Problem 3:

Given a n x n matrix inmatrix of positive integers, print an integer outnum based on the below logic. Identify all possible sets in inmatrix that contain at least four consecutive elements of the same value val, either horizontally, vertically, or diagonally If only one set of consecutive elements is identified, store the value val in outnum If more than one set of consecutive elements is identified, find the smallest value and store it in outnum If no set of four consecutive elements of the same value is identified either horizontally, vertically, or diagonally, print-1 Input Format First line will contain number of rows n of inmatrix. The next n lines will contain the elements of inmatrix. Each line will have n elements separated by space. Constraints 4<=n<=100 Output Format Print outnum. For example in the first sample test case:- Following elements are present consecutively at least four times: Element 3 horizontally in the 5th row. Element 1 diagonally starting from the 2nd column in the first row. Element 6 diagonally starting from the 4th column in the second row. Element 9 vertically in the 6th column. As element 1 is the smallest value of the four identified sets of consecutive values, the output is 1.


Python:

def FreqCheck(arr):

    elements_count = {}

    a=[]

    for element in arr:

        if element in elements_count:

            elements_count[element] += 1

        else:

            elements_count[element] = 1

    for key, value in elements_count.items():

        if(value>=4):

            return key

 

            

def get_rows(grid):

    return [[c for c in r] for r in grid]

def get_cols(grid):

    return zip(*grid)

def get_backward_diagonals(grid):

    b = [None] * (len(grid) - 1)

    grid = [b[i:] + r + b[:i] for i, r in enumerate(get_rows(grid))]

    return [[c for c in r if c is not None] for r in get_cols(grid)]



n=int (input())

grid=[]

for i in range(n):

    grid.append(list(map(int,input().split(" "))))

FinalList=[]    

for i in range(n):

    FinalList.append(FreqCheck(grid[i]))

columns = [[grid[j][i] for j in range(len(grid))] for i in range(len(grid[0]))]

for i in range(len(columns)):

    FinalList.append(FreqCheck(columns[i]))

diagonals=get_backward_diagonals(grid)

for i in range(len(diagonals)):

    FinalList.append(FreqCheck(diagonals[i]))

    

FinalList=[i for i in FinalList if i]

if len(FinalList)==0:

    print('-1')

else:

    print(min(FinalList))

Post a Comment

Previous Post Next Post