Competitive Programming Questions
Competitive 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
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.
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.
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
SAMPLE INPUT
3
4
*.*.
.*.*
*.*.
.*.*
3
.*.
*.*
.*.
3
..*
**.
..*
SAMPLE OUTPUT
NO
BOTH
HORIZONTAL
MEDIUM
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
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.
Output Format:
For each query Q, output the number of communities with even member-count
Constraints:
1<=N<=10^6
1<=I, J<=N
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.
Example:
For, N=1, the grid structure and solution is shown below:
Figure 2.
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.
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
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)
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
1 1
Yes
23
45
57
Input Format:
Input will consist of three parts, viz.
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
3
OOX
OXO
OOX
1
4 Yes
11
12
21
32
3
OOX
OXO
OOX
2
4 No
11
12
21
23
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
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
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).