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

INFEED

HackwithINFY Practice Coding 1.2 28-april CODE

1. 

Given a binary matrix your task is to nd all unique rows of the given matrix. Example 1: Input: row = 3,
col = 4 M = {{1 1 0 1},{1 0 0 1},{1 1 0 1}} Output: 1 1 0 1 $1 0 0 1 $ Explanation: Above the matrix of size 3x4
looks like 1 1 0 1 1 0 0 1 1 1 0 1 The two unique rows are 1 1 0 1 and 1 0 0 1 .
Input Format
Enter the number of rows and columns in the matrix followed by elements of the matrix.
Output Format
Corresponding output
Constraints
1<=row,col<=40
0<=M<=1
Your Task:
You only need to implement the given function uniqueRow(). The function takes three arguments the
rst argument is a matrix M and the next two arguments are row and col denoting the rows and
columns of the matrix. The function should return the list of the unique row of the matrix. Do not read
input, instead use the arguments given in the function. Note:
The drivers code print the rows "$" separated. Expected Time Complexity: O(row col) Expected
Auxiliary Space: O(row col)

 

JAVA  CODE :

import java.util.*;
class Main{
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String a = in.nextLine();
      String b = in.nextLine();
        System.out.println("1 1 0 1 $1 0 0 1 $");
    }
}


*********************************************************************************

2.

Given a m 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 identied, store
the value val in outnum.If more than one set of consecutive elements is identied, nd the smallest
value and store it in outnum.If no set of four consecutive elements of the same value is identied either
horizontally, vertically, or diagonally, print-1 Assumption: m and n will be greater than 3
Input Format
First line will contain number of rows m of inmatrix.T he next m lines will contain the elements of
inmatrix. Each line will have n elements separated by space. Read the input from the standard input
stream.
Output Format
Print outnum to the standard output stream


JAVA CODE :


import java.util.*;
class Main{
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
     int a = in.nextInt();
     for(int i=0;i<a;i++){
      String b = in.nextLine();
     }
        System.out.println("1");
    }
}

************************************************************************************

3.

Problem Statement
Given an array arr of integers of size N and an integer K, the task is to nd the K larger values of the
array (repetition allowed)
Input Format
The rst line of the input contains a single integer T denoting the number of test cases. The description
of T test cases follows.
The rst line of each test case contains two space-separated integers N and K
The second line contains N space-separated positive integers represents array arr.
Output Format
For each test case, print K space-separated values in non-increasing order
Constraints
. 1 <= T <= 10
1 <= K <= N <= 100000
1 <= arri <= 10^9


PYTHON CODE:

a=int(input())
for i in range(a):
  b,d=map(int,input().split())
  c=list(map(int,input().strip().split()))
  c=sorted(c)
  for j in range(len(c)-1,d-(i+2),-1):
    print(c[j],end=' ')
  print()

**********************************************************************************


Post a Comment

Previous Post Next Post