PHD Screening Test Lab
PHD Screening Test Lab
Part I: Laboratory
Q1. Take a 2D array as an input and print the number of rows which have exactly 3 zeros and 3 ones in them.
The input will be provided by first giving the number of rows (n) followed by the number of columns (m) in a single
line. The next m lines will contain n integers separated by a space.
The output should be a single number only, that prints the answer.
Sample Input
58
20110012
22111000
21112111
21010102
11100002
Sample Output
3
Explanation
The following rows contain exactly 3 zeros and exactly 3 ones:
20110012
22111000
21010102
int main(){
int m; //number of rows
int n; //number of columns
int X[100][100]; //2D array declaration
scanf("%d %d", &m,&n); // Reading m and n from STDIN
int i,j; // To iterate over the 2D array to take inputs
for(i=0;i<m;i++) // Iterate over rows
{
for(j=0;j<n;j++) // Iterate over columns
scanf("%d",&X[i][j]); // Reading input for a cell
}
int answer; //store the answer here
/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
Q2. Take a 1D array as an input. Print the distance between the index of the smallest element and the index
of the largest element.
The input will be provided by first giving the number of elements in the array (n). The next line contains n integers
separated by a space.
The output should be a single number only, that prints the answer.
Sample Input
5
21354
Sample Output
2
Explanation
Largest element 5 has an index 3
Smallest element 1 has an index 1
Distance between smallest and largest element index = abs(3-1)=2, where abs() is the absolute value function.
int main(){
int n; //number of elements
int X[100]; //1D array declaration
scanf("%d", &n); // Reading m and n from STDIN
int i; // To iterate over the 1D array to take inputs
for(i=0;i<n;i++) // Iterate over array
{
scanf("%d",&X[i]); // Reading input for a cell
}
int answer; //store the answer here
/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */
Consider the 2D array as follows and the grid “2 5” which is highlighted. There are 2 zeros above it, so the a value is
2. There are no zeros below it, so the b value is 0. There are 2 zeros of the left, so the c value is 2 (note that the third
zero at “2 1” is not considered as it is not consecutive and occurs after a gap of 1). There is one zero on the right, so
the d value is 1. All zeros used in counting are highlighted.
1 1 1 1 1 0 1
1 1 1 0 1 0 1
1 0 1 0 0 0 0
1 0 1 0 0 1 1
1 1 1 1 1 1 1
Given a 2D array and the a-value, b-value, c-value and d-value; find the grid “i j” whose a, b, c and d values match
with the supplied input.
The input will be provided by first giving the number of rows (n) followed by the number of columns (m) in a single
line. The next m lines will contain n integers separated by a space. The next line prints 4 integers, the desired a-value,
b-value, c-value and d-value.
Sample Input
57
1111101
1110101
1010000
1010011
1111111
2021
Sample Output
25
Sample Program to take input and print output
/*
// Sample code to perform I/O:*/
#include <stdio.h>
int main(){
int m; //number of rows
int n; //number of columns
int X[100][100]; //2D array declaration
scanf("%d %d", &m,&n); // Reading m and n from STDIN
int i,j; // To iterate over the 2D array to take inputs
for(i=0;i<m;i++) // Iterate over rows
{
for(j=0;j<n;j++) // Iterate over columns
scanf("%d",&X[i][j]); // Reading input for a cell
}
int a,b,c,d; //desired values
scanf("%d %d %d %d", &a,&b,&c,&d); // Reading m and n from STDIN
int answerX, answerY; //store the answer here
/* Write your code to calculate the answer here without doing input or output because that is already done over
here. You may change any segment of the code above or below. */
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail