0% found this document useful (0 votes)
14 views14 pages

Mock Test 2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

2024_Infosys_MRECW_Power_Programmer_Test 2

Test Summary
 No. of Sections: 2
 No. of Questions: 3
 Total Duration: 90 min
Section 1 - Coding
Section Summary
 No. of Questions: 2
 Duration: 60 min
Additional Instructions:
None
Q1.
Problem Statement

Given a binary matrix of N x M, containing at least a value 1. The task is to find the
distance of the nearest 1 in the matrix for each cell. The distance is calculated as |i1
– i2| + |j1 – j2|, where i1, j1 are the row number and column number of the current
cell and i2, j2 are the row number and column number of the nearest cell having
value 1.

Example:
Input:
N = 3, M = 4
mat[][] = {
0, 0, 0, 1,
0, 0, 1, 1,
0, 1, 1, 0
}

Output:
3210
2100
1001

Explanation:
For cell at (0, 0), the nearest 1 is at (0, 3).
so distance = (0 - 0) + (3 - 0) = 3.
Similarly, all the distance can be calculated.
Input Format
The first line contains two integers, N and M, representing the number of rows and
columns in the matrix.
The next N lines each contain M integers (0 or 1), representing the elements of the
matrix.
Output Format
The output consists of N lines.
Each line contains M integers, where each integer represents the distance of the
nearest cell having 1 for each cell in the matrix.

Refer to the sample output for the formatting specifications.


Constraints
1 <= N, M <= 20
Sample Input Sample Output
2 2 0 1
1 0 1 0
0 1
Sample Input Sample Output
3 4
0 0 0 1 3 2 1 0
0 0 1 1 2 1 0 0
0 1 1 0 1 0 0 1

Time Limit: 10 ms Memory Limit: 256 kb Code Size: 1024 kb


Q2.
Problem Statement

Given an integer array nums and an integer k, return the number of non-empty
subarrays that have a sum divisible by k.

A subarray is a contiguous part of an array.

Example 1

Input:
6
4 5 0 -2 -3 1
5

Output:
7

Explanation:
There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Example 2

Input:
1
5
9

Output:
0

Explanation:
The sum of elements from 1st position to 5th position is 15.
Input Format
The first line contains a single integer n, representing the size of the array nums.
The second line contains n space-separated integers arr[i], representing the elements
of the array nums.
The third line contains a single integer k, representing the divisor.
Output Format
The output displays a single integer representing the number of subarrays in nums
whose sum is divisible by k.

Refer to the sample output for the formatting specifications.


Constraints
1 ≤ n ≤ 10
1 ≤ arr[i] ≤100
1 ≤ k ≤ 1000
Sample Input Sample Output
6 7
4 5 0 -2 -3 1
5
Sample Input Sample Output
1 0
5
9
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Section 2 - Additional Coding


Section Summary
 No. of Questions: 1
 Duration: 30 min
Additional Instructions:
None
Q1.
Problem Statement

You are given an array of n pairs where pairs[i] = [lefti, righti] and lefti < righti. A pair
p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this
fashion. Return the length longest chain that can be formed. You do not need to use
up all the given intervals. You can select pairs in any order.

Example 1

Input:
3
12
23
34
Output: 2
Explanation: The longest chain is [1,2] -> [3,4].

Example 2

Input:
3
12
78
45
Output: 3
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
Input Format
The first line of input consists of an integer n, representing the number of pairs.
Each of the next n lines consists of two space-separated integers lefti and righti,
representing the values of the ith pair.
Output Format
The output prints a single line containing an integer, representing the length of the
longest chain that can be formed from the given pairs.
Constraints
1 ≤ n ≤ 100
-100 ≤ lefti < righti ≤ 100
Sample Input Sample Output
3 2
1 2
2 3
3 4
Sample Input Sample Output
3 3
1 2
7 8
4 5
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Answer Key & Solution
Section 1 - Coding

Q1Test Case

Input Output
3 3
1 0 0 0 1 1
0 0 1 1 1 0
0 1 1 1 0 0

Weightage – 25

Input Output
4 4
1 0 0 1 0 1 1 0
1 0 0 1 0 1 1 0
0 0 0 1 1 2 1 0
0 0 0 0 2 3 2 1

Weightage – 25

Input Output
3 3
0 0 0 2 1 2
0 1 0 1 0 1
0 0 0 2 1 2

Weightage - 25Input
4 3
1 1 1
1 0 0
0 0 1
1 1 1
Output
0 0 0
0 1 1
1 1 0
0 0 0

Weightage – 25

Sample Input
2 2
1 0
0 1
Sample Output
0 1
1 0

Sample Input
3 4
0 0 0 1
0 0 1 1
0 1 1 0
Sample Output
3 2 1 0
2 1 0 0
1 0 0 1

Solution
#include <bits/stdc++.h>
using namespace std;

// Function to print the distance of nearest cell having 1 for each cell.
void printDistance(vector<vector<int>>& mat, int N, int M)
{
vector<vector<int>> ans(N, vector<int>(M, INT_MAX));

// For each cell


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Traversing the whole matrix to find the minimum distance.
for (int k = 0; k < N; k++) {
for (int l = 0; l < M; l++) {
// If cell contains 1, check for minimum distance.
if (mat[k][l] == 1) {
ans[i][j] = min(ans[i][j], abs(i - k) + abs(j - l));
}
}
}
}
}

// Printing the answer.


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int N, M;

// Taking input for dimensions of the matrix.


cin >> N;
cin >> M;

// Initializing the matrix.


vector<vector<int>> mat(N, vector<int>(M));

// Taking input for matrix elements.


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> mat[i][j];
}
}

// Function call
printDistance(mat, N, M);

return 0;
}

Q2Test Case

Input
4
7 8 -2 -4
2

Output
6

Weightage – 10
Input
5
6 8 9 27 32
3

Output
4

Weightage – 10

Input
10
1 4 5 18 28 39 47 56 78 98
3
Output
15

Weightage – 25

Input
8
1 2 3 4 5 6 7 8
49
Output
0

Weightage – 15

Input
9
1 2 3 4 5 6 7 8 9
89
Output
0
Weightage – 25

Input
6
5 7 8 9 4 3
3
Output
9

Weightage – 15

Sample Input
6
4 5 0 -2 -3 1
5
Sample Output
7

Sample Input
1
5
9
Sample Output
0

Solution
import java.util.Scanner;

public class Main {


public static int countSubarrays(int[] nums, int k) {
int count = 0;
int[] remainderFreq = new int[k];
remainderFreq[0] = 1;

int sum = 0;
for (int num : nums) {
sum = (sum + num % k + k) % k;
count += remainderFreq[sum];
remainderFreq[sum]++;
}

return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
int k = scanner.nextInt();

int result = countSubarrays(nums, k);


System.out.println(result);
}
}
Section 2 - Additional Coding

Q1Test Case

Input
4
1 2
2 3
3 4
5 6
Output
3

Weightage – 10

Input
8
1 2
2 3
3 4
5 6
7 8
4 5
6 7
9 10
Output
5
Weightage – 20

Input
6
1 2
3 4
5 6
7 8
9 10
11 12
Output
6

Weightage – 20

Input
10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
Output
10

Weightage – 25

Input
20
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
31 32
33 34
35 36
37 38
39 40
Output
20

Weightage – 25

Sample Input
3
1 2
2 3
3 4
Sample Output
2

Sample Input
3
1 2
7 8
4 5
Sample Output
3

Solution
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Solution {
public static int findLongestChain(int[][] pairs) {
Arrays.sort(pairs, Comparator.comparingInt(o -> o[1]));
int n = pairs.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);

for (int i = 1; i < n; i++) {


for (int j = 0; j < i; j++) {
if (pairs[i][0] > pairs[j][1]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

int longestChain = 0;
for (int len : dp) {
longestChain = Math.max(longestChain, len);
}

return longestChain;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// System.out.print("Enter the number of pairs: ");


int n = scanner.nextInt();
int[][] pairs = new int[n][2];

//System.out.println("Enter the pairs in the format [lefti righti]:");


for (int i = 0; i < n; i++) {
pairs[i][0] = scanner.nextInt();
pairs[i][1] = scanner.nextInt();
}

int longestChain = findLongestChain(pairs);


System.out.println(longestChain);

scanner.close();
}
}
#include <iostream>
#include <algorithm>

class Solution {
public:
static int findLongestChain(int** pairs, int n) {
std::sort(pairs, pairs + n, [](int* a, int* b) {
return a[1] < b[1];
});

int* dp = new int[n];


std::fill(dp, dp + n, 1);

for (int i = 1; i < n; i++) {


for (int j = 0; j < i; j++) {
if (pairs[i][0] > pairs[j][1]) {
dp[i] = std::max(dp[i], dp[j] + 1);
}
}
}

int longestChain = 0;
for (int i = 0; i < n; i++) {
longestChain = std::max(longestChain, dp[i]);
}

delete[] dp;
return longestChain;
}
};

int main() {
int n;
std::cin >> n;
int** pairs = new int*[n];

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


pairs[i] = new int[2];
std::cin >> pairs[i][0] >> pairs[i][1];
}

int longestChain = Solution::findLongestChain(pairs, n);


std::cout << longestChain << std::endl;

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


delete[] pairs[i];
}
delete[] pairs;

return 0;
}

You might also like