0% found this document useful (1 vote)
267 views

Competitive Programming Questions

The document contains 5 competitive programming questions of varying difficulty levels - Easy, Medium, and Hard. The first question asks to toggle the case of letters in a string and output the result. The second asks to count the number of numbers between two ranges that are divisible by a given number. The third asks about determining the winner of a chess tournament based on player abilities. The medium questions include calculating the final single digit sum when repeatedly summing the digits of a large number in run-length encoding, determining if a matrix is symmetric, and finding prime numbers that can be expressed as the sum of consecutive prime numbers. The hard question describes a new zombie game where the player must escape a zombie-infested city
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
267 views

Competitive Programming Questions

The document contains 5 competitive programming questions of varying difficulty levels - Easy, Medium, and Hard. The first question asks to toggle the case of letters in a string and output the result. The second asks to count the number of numbers between two ranges that are divisible by a given number. The third asks about determining the winner of a chess tournament based on player abilities. The medium questions include calculating the final single digit sum when repeatedly summing the digits of a large number in run-length encoding, determining if a matrix is symmetric, and finding prime numbers that can be expressed as the sum of consecutive prime numbers. The hard question describes a new zombie game where the player must escape a zombie-infested city
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

COMPETETIVE PROGRAMMING QUESTIONS

EASY
1) Toggle String
You have been given a String S consisting of uppercase and lowercase English alphabets. You
need to change the case of each alphabet in this String. That is, all the uppercase letters should be
converted to lowercase and all the lowercase letters should be converted to uppercase. You need
to then print the resultant String to output.

Input Format
The first and only line of input contains the String S

Output Format
Print the resultant String on a single line.

Constraints
1|S|100

where |S| denotes the length of string S.

2) Count Divisors
You have been given 3 integers l, r and k. Find how many numbers between l and r (both
inclusive) are divisible by k. You do not need to print these numbers, you just have to find their
count.

Input Format
The first and only line of input contains 3 space separated integers l, r and k.

Output Format
Print the required answer on a single line.

Constraints
1lr1000
1k1000
SAMPLE INPUT
1 10 1
SAMPLE OUTPUT
10
3) Chess Tournament
2N participants (P1 , P2 , P3 .... , P2N ) have enrolled for a knockout chess tournament. In the first
round, each participant P2k-1 is to play against participant P2k, (1 k 2N-1) . Here is an example
for k = 4 :

Some information about all the participants is known in the form of a triangular matrix A with
dimensions
(2N-1) X (2N-1). If Aij = 1 (i > j), participant Pi is a better player than participant Pj, otherwise Aij
= 0 and participant Pj is a better player than participant Pi. Given that the better player always
wins, who will be the winner of the tournament?

Note : Being a better player is not transitive i.e if Pi is a better player than Pj and Pj is a better
player than Pk, it is not necessary that Pi is a better player than Pk .

Input
The first line consists of N. Then, 2N-1 lines follow, the ith line consisting of i space separated
integers Ai+1 1 , Ai+1 2 , .... Ai+1 i

Output
A single integer denoting the id of the winner of the tournament.

Constraints
1 N 10
Aij = {0, 1} (i > j)

SAMPLE INPUT
2
0
10
011
SAMPLE OUTPUT
1
EXPLANATION
When 1 plays against 2, 1 wins. When 3 plays against 4, 4 wins. When 1 plays against 4, 1 wins.
So, 1 wins the tournament.

4) Recursive Sum

Little Bear has received a home assignment to find the sum of all digits in a number N.
Following his affinity towards single digit number, he intends to repeatedly compute the sum of
all digits until the sum itself becomes a single digit number.

Can you write a program to compute the final single-digit sum?

As the number N is very big, it is given in the following run length encoded format - N is
represented as a sequence of M blocks, where each block i (0 i < M) is represented by two
integers - (len[i], d[i]). This implies that the digit d[i] occurs len[i] number of times.

For example, {(2,1), (1,2), (2,9)} represents the number 11299.

Input
The first line contains a single integer T, denoting the number of test cases.
Each test case starts with a single integer M, which is the number of following run-length
encoded blocks.
Each of the next M lines contains two space separated integers: len[i] and d[i].

Output
Print the single-digit sum for each of the T test cases in a new line.

Constraints
0 d[i] 9
1 len[i] 1015
1 M 104
1 T 10

5) Matrix Symmetry

You are given a square matrix of size $$n$$. Rows are indexed $$1$$ to $$n$$ from top to
bottom and columns are indexed $$1$$ to $$n$$ form left to right. Matrix consists of only '*'
and '.'. You need to check whether matrix is symmetric or not. if it is, check it is symmetric about
vertical axis or horizontal axis or both.

A matrix is said to be symmetric about horizontal axis if $$1st$$ row is identical to $$n \; th$$
row, $$2nd$$ is identical to $$(n-1)\; th$$ row and so on...
A matrix is said to be symmetric about vertical axis if $$1st$$ column is identical to nth column,
$$2nd$$ identical to $$(n-1) \; th$$ and so on for all columns.

INPUT

First line contains $$t$$, the number of test cases. First line of each test case contains $$n$$ the
size of matrix. Each of next $$n$$ lines contain $$n$$ characters.

OUTPUT

Output t lines, answer for each test case. Print "HORIZONTAL" if symmetric about horizontal
axis. Print "VERTICAL" if symmetric about vertical axis. Print "BOTH" if symmetric about
both axes. Print "NO" if it is not symmetric.

Constraints

$$1 < t < 500$$


$$1 < n < 50 $$

SAMPLE INPUT
3
4
*.*.
.*.*
*.*.
.*.*
3
.*.
*.*
.*.
3
..*
**.
..*
SAMPLE OUTPUT
NO
BOTH
HORIZONTAL
MEDIUM

1) Consecutive Prime Sum


Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example

5=2+3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this property are present in the
range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out number of prime numbers that satisfy the above mentioned property in a
given range.

Input Format:
First line contains a number N

Output Format:
Print the total number of all such prime numbers which are less than or equal to N.

Constraints:
1. 2<N<=12,000,000,000

Sample Input and Output

SNo. Input Output Comment

(Below 20, there are 2 such


1 20 2 numbers: 5 and 17).
5=2+3
17=2+3+5+7
2 15 1

2) Online Communities Even Groups

In a social network, online communities refer to the group of people with an interest towards the
same topic. People connect with each other in a social network. A connection between
Person I and Person J is represented as C I J. When two persons belonging to different
communities connect, the net effect is merger of both communities which I and J belonged to.

We are only interested in finding out the communities with the member count being an even
number. Your task is to find out those communities.
Input Format:
Input will consist of three parts, viz.

1. The total number of people on the social network (N)


2. Queries
C I J, connect I and J
Q 0 0, print the number of communities with even member-count
-1 will represent end of input.

Output Format:
For each query Q, output the number of communities with even member-count

Constraints:
1<=N<=10^6
1<=I, J<=N

Sample Input and Output

SNo. Input Output

5
Q00
C12
0
Q00
1 1
C23
0
Q00
1
C45
Q00
-1

3) 3D Maze Problem

Suppose, an ant is trapped in a maze, with only one way in and one way out.

The maze is a cubic-lattice like structure of dimension NxNxN (Length=Breadth=Height=N).


The way in is the left-bottom most point, and the way out is the right top-most point (along the
principal diagonal). The below picture shows the maze for N=2.
Figure 1.
Assuming the ant only moves right, forward or upwards along the grids of the maze, calculate
the total number of ways in which the ant can escape. Mathematically right, forward and
upwards are defined at positive changes in co-ordinates on x, y and z axes respectively.

Example:
For, N=1, the grid structure and solution is shown below:

Figure 2.

Thus, for N=1, we have a net of 6 ways.


Input Format:
Single integer N

Output Format:
Output also consists of a single number corresponding to the number of ways the ant can escape
the maze.

Constraints:
0<N<=8

Example
Example
Sample Input Sample Output
Number

1 1
6

2 2
90

4) Even Sum
A number is even if it is divisible by 2, but in this case a number is even if the active bits (1s in
its binary representation) of a given number are 2. So your task is to find the sum of first N even
numbers.

Input Format:
First line of the input contains an integer T denoting the number of test cases.

Each of the next T lines contains a single integer N.

Output Format:
For each test case, print a single integer denoting sum of first N even numbers mod
1000000007.

Constraints:
0 < N <=10^5

Example:
Example
Sample Input Sample Output
Number

1 1
8
2

2 315
2
666
15
25

5) Box in a Box
Idea is to take a number as input and print a pattern of boxes
If input is 2, two boxes are to be printed - one inside the other
Smallest box will be of size 3*3, the next bigger box will be 5*5, the next one will be 7*7, so on
and so forth
For input 1, then draw a box of dimensions 3*3
For input 2, outer box will be 5*5, inner will be 3*3
For input 3, outer box will be 7*7, with 2 more inner boxes
So for n, outermost box will be n*2 +1 in size, with (n-1) inner boxes
All boxes will be top left aligned as shown in the figure

Input Format:
First line of input contains a number N

Output Format:
Print N nested boxes

Constraints:
0 < N < 25

Sample Input and Output


Example
Sample Input Sample Output
Number

1 2

2 3
HARD

1) Zombie World
Zoya has developed a new game called Zombie World. The objective of the game is to kill all
zombies in given amount of time. More formally,
N represents the total number of zombies in the current level
T represents the maximum time allowed for the current level
P represents the initial energy level a player starts with
Ei defines the energy of the i-th zombie
D defines the minimum energy the player needs, to advance to the next level
When a player energy is greater than or equal to the i-th zombie's energy, the player wins. Upon
winning, the player will be awarded with an additional energy equal to the difference between
current zombie energy and the player energy.
One unit of time will be taken to complete the fight with a single zombie.
Rules of the game:-
At any given time, a player can fight with only one zombie
Player is allowed to choose any one zombie to fight with.
Your task is to determine whether the player will advance to the next level or not, if he plays
optimally.

Input Format:
The first line contains the number of test cases (K)

Each test case consists of three parts:


1. The total number of zombies (N) and the maximum time allowed (T)
2. Array of size N, which represents the energy of zombies (E)
3. The initial energy level a player (P) and the minimum energy required to advance (D)

Output Format:
Print "Yes" if a player can advance to the next level else print "No".

Constraints:
1<=K<=10
1<=N<=50
1<=Ei<=500
1<=T<=100
1<=D<=2000
1<=P<=500

Sample Input and Output

SNo. Input Output

1 1
Yes
23
45
57

2) Trace the Rats


Given a square maze (A) of dimension N, every entry (Aij) in the maze is either an open
cell 'O' or a wall 'X'. A rat can travel to its adjacent locations (left, right, top and bottom), but to
reach a cell, it must be open. Given the locations of Rrats, can you find out whether all the rats
can reach others or not?

Input Format:
Input will consist of three parts, viz.

1. Size of the maze (N)


2. The maze itself (A = N * N)
3. Number of rats (R)
4. Location of R rats (Xi, Yi)

Note:
(Xi,Yi) will represents the location of the i-th rat.
Locations are 1-index based.

Output Format:
Print "Yes" if the rats can reach each other, else print "No"

Constraints:
1<=N<=350
Aij = {'O','X'}
1<=R<=N*N
1<=Xi<=N
1<=Yi<=N

Sample Input and Output

SNo. Input Output

3
OOX
OXO
OOX
1
4 Yes
11
12
21
32
3
OOX
OXO
OOX
2
4 No
11
12
21
23

3) Stable Marriage Problem


There are given n men and n women. Each woman ranks all men in order of her preference (her
first choice, her second choice, and so on). Similarly, each man sorts all women according to his
preference. The goal is to arrange n marriages in such a way that if a man m prefers some woman
w more than his wife, and w prefers m more then her husband a new marriage occurs between w
and m. If w prefers her husband more, then she stays married to him. This problem always has a
solution and your task is to find one.

Input
The first line contains a positive integer t<=100 indicating the number of test cases. Each test
case is an instance of the stable marriage problem defined above. The first line of each test case
is a positive integer n<=500 (the number of marriages to find). The next n lines are the woman's
preferences: ith line contains the number i (which means that this is the list given by the ith
woman) and the numbers of men (the first choice of ith woman, the second choice,...). Then, the
men's preferences follow in the same format.

Output
For each test case print n lines, where each line contains two numbers m and w, which means that
the man number m and the woman number w should get married.

Example

Input:
2
4
14312
22134
31342
44312
13241
22314
33124
43241
7
13421675
26423517
36357241
41632475
51653472
61734562
75624371
14537261
25647321
31654372
43567241
51764352
66375241
71742653

Output:
13
22
31
44
14
25
31
43
57
66
72

Warning: For large Input/Output data, be careful with certain languages

4) N Knights Problem
Dave recently mastered the problem of placing N queens on a chessboard so that no two queens
attack eachother. Now he wants to see how many knights he can place on a chessboard so that no
two knights attack eachother. Normally this would be a simple task, but some of the squares of
the chessboard have been marked as unusable and hence cannot have a knight placed on them.

Recall that a knight can attack another knight if their vertical distance is 2 and their horizontal
distance is 1, or if their vertical distance is 1 and their horizontal distance is 2. Only one knight
may be placed on each square of the chessboard

Input

The first line of input contains an integer T (0<T50), the number of test cases to follow.

Each test case will begin with a line containing 2 integers M and N (0<M,N30), denoting the
number of rows and columns, respectively. M lines follow, each containing exactly N characters.
The j-th character of the i-th line is '.' if a knight may be placed in the j-th column of the i-th row,
and '#' if the square is unusable.

Output

For each test case, output on a single line the maximum number of knights that may be placed on
the chessboard such that no two attack eachother.

Sample input:

2
24
....
....
55
..#..
#..#.
##...
...##
.....

Sample output:

4
11

The following image represents the chessboard and a possible solution to the second test case:

5) Physics Class
The Physics teacher in Tanu's class is teaching concepts of a bouncing ball. The rubber ball that
she is using has the property that if the ball is dropped from height H then, it bounces back to
maximum height H/F. So after first bounce it rises up to maximum height H/F, after second
bounce to maximum height H/F2, after third bounce to maximum height H/F3, and so on.
The class has N children, and the teacher wants to select two children such that when the taller
child drops the ball from his height, ball reaches a maximum height equal to the height of the
shorter child after some (possibly zero) number of bounces. Note that zero bounces mean heights
of the two children will be same. Given the heights of the children in the class Height[i], can you
tell how many ways are there for the teacher to select two children for the demonstration? Note
that when heights are same, the pair is only counted once (See first example for further
clarification).

Input

First line contains T, number of testcases. Then 2*T lines follow, 2 per testcase.
First line of each testcase consists of two space-separated intergers N and F. Second line of each
testcase contains N space-separated integers representing the array Height.

Output

For each testcase, print a single line containing the answer to the problem.

Constraints

For 40 points: 1 T 100, 1 N 103, 2 F 109, 1 Height[i] 109


For 60 points: 1 T 100, 1 N 104, 2 F 109, 1 Height[i] 109

Example

Input:

2
32
222
32
214

Output:
3
3

Explanation:
In the first case, the three pairs are (child 1, child 2), (child 2, child 3) and (child 1, child
3).
For the second case also, the three pairs are (child 1, child 2), (child 2, child 3) and (child
1, child 3).

You might also like