Mock Test 2
Mock Test 2
Mock 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.
Given an integer array nums and an integer k, return the number of non-empty
subarrays that have a sum divisible by k.
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.
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));
int main()
{
int N, M;
// 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;
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();
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);
int longestChain = 0;
for (int len : dp) {
longestChain = Math.max(longestChain, len);
}
return 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 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];
return 0;
}