Problem Practice
CIE-20 Marks Component
Class - Computer Engineering/Information Technology(B)
Easy Problem Statement
1. Find all possible permutations in which n people can occupy r seats in a theater
N friends are planning to go to a movie. One among them suggested few movies and all others
started to discuss and finally they selected a movie. One among them quickly booked their tickets
online, to their surprise they are unable to select their seats. All of them got confused. Then anyhow,
decided to go to the movie. They rushed to reach the theater on time. Again they are surprised that
no one was there in the theater. They are the only people about to watch the movie. There is r
number of seats in which, n number persons should sit. In how many ways they can sit inside the
theater.?
Given the number of people n and the number of seats r as input. The task is to find the different
number of ways in which n number of people can be seated in those r number of seats.
For example,
Input:
Number of people: 5
Number of Rows: 3
Output:
The total number of ways in which n people can be seated in r seats = 60.
2. Program to remove all characters in a string except alphabets
For example, consider the following example
Input and Output Format:
Input consists of a string. Assume the maximum length of the string is 200. The characters in the
string can contain both uppercase, lowercase, and symbols.
Sample Input:
pro$#&gra7m
Sample Output:
Program.
3. Given an N * N binary maze where a 0 denotes that the position can be visited and a 1 denotes that
the position cannot be visited without a key, the task is to find whether it is possible to visit the
bottom-right cell from the top-left cell with only one key along the way. If possible then print “Yes”
else print “No”.
Example:
Input: maze[][] = {
{0, 0, 1},
{1, 0, 1},
{1, 1, 0}}
Output: Yes
4. Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix arr is shown below:
123
456
989
The left-to-right diagonal = 1+5+9=15. The right to left diagonal = .3+5+9=17. Their absolute
difference is .|15-17|=2.
Moderate Problem Statement
1. You are in charge of the cake for your niece's birthday and have decided the cake will have one
candle for each year of her total age. When she blows out the candles, she’ll only be able to blow
out the tallest ones. Your task is to find out how many candles she can successfully blow out.
For example, if your niece is turning 4 years old, and the cake will have 4 candles of height 4, 4, 1, 3,
she will be able to blow out 2 candles successfully, since the tallest candles are of height 4 and
there are 2 such candles.
Input Format
The first line contains a single integer, n, denoting the number of candles on the cake.
The second line contains n space-separated integers, where each integer i describes the height of
candle i.
Output Format
Return the number of candles that can be blown out on a new line.
Sample Input
4
3213
Sample Output
2
2. You are choreographing a circus show with various animals. For one act, you are given two
kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).
- The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump.
- The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump.
You have to figure out a way to get both kangaroos at the same location at the same time as part of
the show. If it is possible, return YES, otherwise return NO.
For example, kangaroo 1 starts at x1=2 with a jump distance v1=1 and kangaroo 2 starts at x2=1
with a jump distance of v2=2 . After one jump, they are both at x=3, (x1+v1 = 2+1, x2+v2 = 1+2 ), so
our answer is YES.
Sample Input
0342
Sample Output
YES
Explanation
The two kangaroos jump through the following sequence of locations:
From the image, it is clear that the kangaroos meet at the same location (number 12 on the
number line) after same number of jumps (4 jumps), and we print YES.
3. Given the time in numerals we may convert it into words, as shown below:
5:00 - five o’ clock
5:01 - one minute past five
5:10 - ten minutes past five
5:15 - quarter past five
5.30 - half past five
5:40 - twenty minutes to six
5:45 - quarter to six
5:47 - thirteen minutes to six
5:28 - twenty eight minutes past five
At minutes=0, use o' clock. For 1<= minutes <=30, use past, and for 30<minutes use to. Note the
space between the apostrophe and clock in o' clock. Write a program which prints the time in
words for the input given in the format described.
Sample Input
5
47
Sample Output
thirteen minutes to six
4. Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the
diagram below, the red region denotes his house, where s is the start point, and t is the
endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You
can assume the trees are located on a single point, where the apple tree is at point a , and
the orange tree is at point b.
When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x -axis. A
negative value of d means the fruit fell units to the tree's left, and a positive value of d means it
falls d units to the tree's right.
Given the value of d for m apples and oranges, determine how many apples and oranges will fall on
Sam's house (i.e., in the inclusive range
)?
For example, Sam's house is between s=7 and t=10 . The apple tree is located at a=4 and the
orange at b=12. There are m=3 apples and n=3 oranges. Apples are thrown apples=[2,3,-4] units
distance from a, and oranges = [3,-2,-4] units distance. Adding each apple distance to the position
of the tree, they land at [4 + 2, 4 + 3, 4 + -4] = [6,7,0]. Oranges land at [12+3, 12 + -2, 12+ -4] =
[15,10,8] . One apple and two oranges land in the inclusive range e 7-10 so we print
1
2
Sample Input 0
7 11
5 15
32
-2 2 1
5 -6
Sample Output 0
1
1
Explanation 0
The first apple falls at position 5-2=3.
The second apple falls at position 5+2=7 .
The third apple falls at position 5+1 = 6.
The first orange falls at position 15+5=20.
The second orange falls at position 15-6 = 9.
Only one fruit (the second apple) falls within the region between 7 and 11, so we print 1 as our first
line of output.
Only the second orange falls within the region between 7 and 11, so we print 1 as our second line
of output.
Hard Problem Statement
1. Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of
the Programmer (the 256th day of the year) during a year in the inclusive range from 1700 to 2700.
From 1700 to 1917, Russia's official calendar was the Julian calendar; since 1919 they used the
Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in
1918, when the next day after 31st January was February 14th. This means that in 1918, February
14th was the 32nd day of the year in Russia.
In both calendar systems, February is the only month with a variable amount of days; it has 29 days
during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible
by 4; in the Gregorian calendar, leap years are either of the following:
- Divisible by 400.
- Divisible by 4 and not divisible by 100.
Given a year, y, find the date of the 256th day of that year according to the official Russian calendar
during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the
two-digit month, and yyyy is y.
For example, the givenyear = 1984. 1984 is divisible by 4, so it is a leap year. The 256th day of a leap
year after 1918 is September 12, so the answer is 12.09.1984.
Sample Input
2017
Sample Output
13.09.2017
Explanation
In the year y=2017, January has 30 days, February has 28 days, March has 31 days, April has 30
days, May has 31 days, June has 30 days, July has 31 days, and August has 31 days. When we sum
the total number of days in the first eight months, we get 31+28+31+30+31+30+31+31=243. Day of
the Programmer is the 256th day, so then calculate 256-243=13 to determine that it falls on day 13
of the month 9th (September). We then print the full date in the specified format, which is
13.09.2017.
2. Given a set of distinct integers, print the size of a maximal subset of S where the sum of any 2
numbers in S’ is not evenly divisible by k.
For example, the array S = [19,10,12,10,24,25,22] and k=4. One of the arrays that can be created is
S’[0] = [10,12,25] . Another is S’[1] = [19,22,24]. After testing all permutations, the maximum
length solution array has 3 elements
Sample Input
43
1724
Sample Output
Explanation
The sums of all permutations of two elements from
are:
1+7=8
1+2=3
1+4=5
7+2=9
7 + 4 = 11
2+4=6
We see that only S’ = {1,7,4} will not ever sum to a multiple of k=3 .
3. David has several containers, each with a number of balls in it. He has just enough containers to
sort each type of ball he has into its own container. David wants to sort the balls using his sort
method.
As an example, David has n=2 containers and 2 different types of balls, both of which are numbered
from 0 to n-1=1. The distribution of ball types per container are described by an n x n matrix of
integers, M[Container][Type]. For example, consider the following diagram for M[[1,4],[2,3]] :
In a single operation, David can swap two balls located in different containers.
The diagram below depicts a single swap operation:
David wants to perform some number of swap operations such that:
- Each container contains only balls of the same type.
- No two balls of the same type are located in different containers.
You must perform q queries where each query is in the form of a matrix, M. For each query, print
Possible on a new line if David can satisfy the conditions above for the given matrix. Otherwise,
print Impossible.
Sample Input
221 11 120 21 1
Sample Output
PossibleImpossible
Explanation
We perform the following q=2queries:
- The diagram below depicts one possible way to satisfy David's requirements for the first query:
Thus, we print Possible on a new line.
- The diagram below depicts the matrix for the second query.
No matter how many times we swap balls of type t0 and t1 between the two containers, we'll
never end up with one container only containing type t0 and the other container only containing
type t1.Thus, we print Impossible on a new line.
4. To sort a given array of strings into lexicographically increasing order or into an order in which the
string with the lowest length appears first, a sorting function with a flag indicating the type of
comparison strategy can be written. The disadvantage with doing so is having to rewrite the
function for every new comparison strategy.
A better implementation would be to write a sorting function that accepts a pointer to the function
that compares each pair of strings. Doing this will mean only passing a pointer to the sorting
function with every new comparison strategy.
Given an array of strings, you need to implement a string_sort function which sorts the strings
according to a comparison function, i.e, you need to implement the function :
void string_sort(const char **arr,const int cnt, int
(*cmp_func)(const char* a, const char* b)){
}
The arguments passed to this function are:
- an array of strings : arr
- length of string array: count
- pointer to the string comparison function: cmp_func
You also need to implement the following four string comparison functions:
1. int lexicographic_sort(char*, char*) : to sort the strings in
lexicographically non-decreasing order.
2. int lexicographic_sort_reverse(char*, char*) : to sort the strings in
lexicographically non-increasing order.
3. int sort_by_number_of_distinct_characters(char*, char*) : to
sort the strings in non-decreasing order of the number of distinct characters present in them. If two
strings have the same number of distinct characters present in them, then the lexicographically
smaller string should appear first.
4. int sort_by_length(char*, char*) : to sort the strings in non-decreasing
order of their lengths. If two strings have the same length, then the lexicographically smaller string
should appear first.
Sample Input
4
Wkue
Qoi
Sbv
fekls
Sample Output
Fekls
Qoi
Sbv
Wkue
Wkue
Sbv
Qoi
fekls
Qoi
Sbv
Wkue
Fekls
Qoi
Sbv
Wkue
fekls