Ptest 2020
Ptest 2020
INSTRUCTIONS
1. Please make sure that your programs adhere strictly to the specified input
and output format.
3. In order to pass, you will have to write correct programs for at least 2 out
of the following 3 problems.
1. Light Bulb Game. We have a set of n (n ≥ 1) light bulbs in a room, each labeled with a unique number
between 1 and n. All light bulbs are initially in the OFF state. A group of n (n ≥ 1) children, c1 , c2 , . . . , cn
enter the room one by one, in order. Child ck (1 ≤ k ≤ n) toggles the state of each light bulb (ON to OFF
/ OFF to ON, depending on the current state of the bulb), whose number is a multiple of k. Note that the
child does not touch any other light bulb. Thus, the child c1 enters the room first, and changes all light bulbs
to the ON state. The next child, c2 , enters the room and turns OFF bulb numbers 2, 4, 6, 8 . . .. Similarly,
child c3 changes the state of bulb numbers 3, 6, 9 . . .; bulbs 3, 9, . . . are turned OFF, while bulbs 6, 12, . . .
are turned back ON. The game ends when cn leaves the room.
Write a program to read as input a number n from the terminal, and print which out of the n bulbs remain
ON after the game ends.
Sample input 1:
10
Sample output 1:
A list of numbers, each in a separate line, corresponding to the bulbs which remain ON after the 10th child
departs.
Sample input 2:
100
Sample output 2:
A list of numbers, each in a separate line, corresponding to the bulb numbers which remain ON after the
100th child departs.
2. Recall how a group of children (say c1 , c2 , . . . , cp ) playing hide-and-seek choose the thief. They stand in a
circle, and one particular child ck (called the counter, say) recites some kind of a rhyme consisting of m ≥ 1
words, starting with himself / herself and moving on to the next child (ck+1 , ck+2 , . . . in order) after reciting
each word. Because the children are standing in a circle, the counter moves to c1 after reaching cp . The child
at whom the rhyme ends is eliminated from subsequent rounds of counting. Counting for the next round starts
1
from the child following the eliminated child (in the circular order described above). The process continues
until one child remains.
Write a program to simulate the above activity, and to determine the order in which children are eliminated.
Input format: An integer (say n) specifying the number of test cases, followed by n lines, each of which
corresponds to a test case. These lines will each consist of 3 integers, corresponding to the total number
of children (p), the serial number of the child chosen as the counter (k), and the length of the rhyme (m),
respectively. Note that numbering of children starts from 1.
Output format: Your program should print the serial numbers of the children in the order in which they are
eliminated, and finish with the number of the child who becomes the thief. For each test case, your program
should print the output in a single line.
Sample input 1:
3
4 1 10
526
523
Sample output 1:
2314
24315
42135
Sample input 2:
4
14 1 10
15 2 4
15 3 2
20 1 1
Sample output 2:
10 6 3 1 14 2 5 9 4 13 8 11 7 12
5 9 13 2 7 12 3 10 1 11 6 4 8 15 14
4 6 8 10 12 14 1 3 7 11 15 5 13 9 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
3. The quality of research publications of a scientist is often characterized by his citation metrics. Every research
publication has a citation count that represents the number of times it has been referred in other research
publications, thereby indicating its credit. The h-index is one of the popular citation metrics that is defined
as the maximum value of h such that the given scientists has published h papers that have each been cited at
least h number of times. Suppose you are given the citation counts of the research publications of a scientist
in chronological order (i.e., following the order in which they were published). Write a program to compute
the h-index of the scientist.
Input Format
Input will be provided as two lines: the first line will contain a single integer n ≥ 1 specifying the number of
2
publications of the scientist; the second line will contain a list of n positive numbers (separated by spaces),
that represent the citation counts of the scientist’s research publications in chronological order.
Output Format
Your program should print a single integer, the h-index, to standard output.
Sample Input 1:
7
10 2 1 5 11 4 3
Sample Output 1:
4
Sample Input 2:
17
11111111111111111
Sample Output 2:
1
Sample Input 3:
2
200 310
Sample Output 3:
2
Sample input 4:
20
12 3 49 11 26 2 31 1 6 88 9 57 26 5 51 0 3 33 1 21
Sample output 4:
11
Sample input 5:
7
10 2 1 5 11 4 3
Sample output 5:
4
Sample input 6:
55
4 10 10 21 21 12 5 13 0 0 0 1 1 0 2 29 0 3 9 27 1 0 1 48 1 0 0 0 8 169 33 40 47 30 14 7 0 0 66 33 70 1 0 185 0 4
38 1 55 6 10 88 105 4 0
Sample output 6:
18