Professional Documents
Culture Documents
Bonus Problems
Bonus Problems
These problems will not be graded. They are just meant for practice. All the problems have
a rating. There are 7 possible ratings(in increasing order of difficulty):
Very Easy
Easy
Medium
Hard
Very Hard
Very Very Hard
Nearly Impossible
The rating for each problem is subjective and disputable. Therefore, it is only a guideline.
If you find any errors in a problem, please send a mail at prakban@
Contributions for problems are also welcome. Also send a mail if you solve a very very hard/nearly
impossible problem! Please write a brief explanation of your solution or write good comments
in the code when sending the solution.
Note:
Solutions to programming questions are acceptable only when they don’t exceed a particular
time limit. The time limit for each question is 1 second for each sample input, unless stated
otherwise.
Please try your solution for each sample input before thinking it is correct. Difficulty
labels for problems are not perfect, but still quite accurate.
1
Flowcharts
Try to understand what the following flowcharts compute. Then write a simple explanation of
the algorithm. For B6 students only: Mail your solution at prakban@
IMPORTANT: The explanation should be as simple and to the point as possible. For example,
if you’re asked about a program which takes as input N and prints N 2 , then there can be many
explanations:
The given flowchart is a flowchart for an algorithm which asks the user for an input. Then
it stores the given number as a variable N. Then the algorithm multiplies N by N and stores
it in another variable M. Then the algorithm prints the number M so that the user can see
it. ××
Computes and prints N 67 +N 45
N 65 +N 43 ×
Computes and prints the product of N with itself X
Computes and prints N 2 XX
2
(Very Hard)
START
NO
YES
YES
YES
SET stack[level]=stack[level]+1
SET stack[j]=0 YES
SET j=j+1 SET j=level-1 IS level==-1?
NO
YES
SET j=j-1
YES
NO
IS stack[j]==stack[level]
OR (stack[j]-stack[level])==(j-level)
OR (stack[j]-stack[level])==(level-j)?
3
Patterns
$./a.out
$./a.out 10
5 *
***
* *****
*** *******
***** *********
******* ***********
********* *************
***************
*****************
*******************
$./a.out
$./a.out 10
5 *******************
*****************
********* ***************
******* *************
***** ***********
*** *********
* *******
*****
***
*
4
3. (Easy) Write a program to print the following pattern:
$./a.out
$./a.out 10
5 *
***
* *****
*** *******
***** *********
******* ***********
********* *************
***************
*****************
*******************
$./a.out
$./a.out 10
5 *
* *
* * *
* * * *
* * * *
* * * *
********* * *
* *
* *
*******************
5
5. (Easy) Write a program to print the following pattern:
$./a.out
$./a.out 10
5 *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
$./a.out
$./a.out 10
5 ********** **********
********* * *********
***** ***** ******** * * ********
**** * **** ******* * * * *******
*** * * *** ****** * * * * ******
** * * * ** ***** * * * * * *****
* * * * * * **** * * * * * * ****
*** * * * * * * * ***
** * * * * * * * * **
* * * * * * * * * * *
6
7. (Medium) Write a program to print the following pattern:
$./a.out
*
* *
*
$./a.out
* *
* *
3 * *
*
* * *
* * * *
* * *
* * * *
* * * *
* * *
* * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
7
8. (Hard) Write a program to print an ellipse, given the radii(in number of stars) X and Y, along
the x and y axes respectively(total width of the ellipse should be 2X+1 and height should be
2Y+1 in number of stars). Slight differences with the sample output are okay:
$./a.out $./a.out
10 10 20 10
******* *************
*********** *********************
*************** ***************************
***************** *******************************
***************** ***********************************
******************* *************************************
******************* ***************************************
********************* ***************************************
********************* *****************************************
********************* *****************************************
********************* *****************************************
********************* *****************************************
********************* *****************************************
********************* ***************************************
******************* ***************************************
******************* *************************************
***************** ***********************************
***************** *******************************
*************** ***************************
*********** *********************
******* *************
$./a.out
30 10
*******************
*********************************
*****************************************
***********************************************
***************************************************
*******************************************************
*********************************************************
***********************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
***********************************************************
*********************************************************
*******************************************************
***************************************************
***********************************************
*****************************************
*********************************
*******************
9. (Hard) Write a program to take as input a number N and then read N lines. Each of the N lines
will have two numbers X and Y. For each line, print the border of a rectangle with width 2X+1
and height 2Y+1(in number of stars). All rectangles should be centered at the same point. The
output should be exactly like the sample output(assume that N<10):
$./a.out
$./a.out 2
10 6
58
1
10 6
***********
* *
*********************
*********************
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
* *
* * * *
*********************
*********************
* *
***********
$./a.out
3
30 5
20 10
10 7
*****************************************
* *
* *
* ********************* *
* * * *
*************************************************************
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
*************************************************************
* * * *
* ********************* *
* *
* *
*****************************************
10. (Very Hard) Imagine a cube of side 2L+1 with its center at the origin and its edges parallel to
the x, y, z axes. This cube is first rotated by angle θ1 about the x-axis and then by angle θ2
about the y-axis. Write a program to take as input L, θ1 and θ2 and print the projection of the
cube on the xy plane. All dimensions are in number of stars and angles are in radians. Slight
differences with the sample output are okay:
$./a.out
7 0.0 0.0
*********
**** ****
* * ** **** ***
* * * ** **
* * ** ***** ***
* * ** ** ** **
* * * * *********
* * ** ** ** **
* * * * * *
* * ** ** ** **
* * ********* * *
* * ** ** ** **
* * *** ***** **
* * *** *** *
* * ** *** **
**** ****
*********
$./a.out $./a.out
***************** *****************
* ** * ** *** * *** *
* ** * ** ** ** ** **
* ** * ** ***************** **
* ***************** * * * *
* * * * * * * *
** * ** * * * * *
* ** * ** ** * ** *
* * * * ** * ** *
* * * * * * * *
* * * * * * * *
** * ** * * ** * **
** ** ** ** * * * *
* * * * * * * *
***************** * * * * *
** * ** * * *****************
** * ** * ** ** ** **
** ** ** ** * *** * ***
***************** *****************
11. (Very Very Hard) The Archimedian Spiral is represented by the polar equation r = aθ. Write a
program to take as input a and φ and print the Archimedian Spiral for θ ∈ [0, φ](all angles are
in radians, all coordinates are in number of stars from origin). Slight differences with the sample
output are okay:
$./a.out
1.5 10.0
******
**** *****
*** ***
*** **
** **
*********** **
** *** **
** ** *** *** *
** * ** * *
** ** * ** **
* * ** ** *
** ** * * *
* * ** ***** * *
* ****** * * * * * *
* ** * * * ** ** * *
** * * * * * ** *
* * ** * * * * *
* ** * * ** ** **
* ** ** * ** ** *
* * * ** ******* *
* * ** * **
** ** ** ** **
* ** ** ** **
* **** *** ** **
***** ****
****
11
Arrogant bosses and Temporary Variables
Suppose you perform very well in ESC101, become very interested in programming and join a
Software Company after graduating from IIT Kanpur. Now you write programs for a living.
One day, you are writing a program which takes two numbers a and b as input from the user
and then does some operations so that:
#include<stdio.h>
int main() {
int a, b;
scanf( "%d%d" , &a, &b);
int tmp;
tmp = a*b;
a = b;
b = tmp;
return 0;
}
Your Boss(Hari Sadu) sees this program and becomes mad at you. The reason he got mad at
you was that you are declaring a new variable tmp and wasting the company’s valuable resources.
He explains to you that that same thing can be done without declaring the new variable tmp.
He tells you that you can replace
int tmp;
tmp = a*b;
a = b;
b = tmp;
by
b = a*b;
a = b/a;
He tells you that this code has the benefit of not using any storage apart from the already
existing variables. You realize that you have been making a huge mistake for the past few years.
In order to correct the mistake, you dig into old programs that you have written. Now your job
is to go through these and rewrite them without using temporary variables.
12
1. (Easy) You find an old program you wrote to swap the values of two numbers, i.e. do the
following:
New value of a = Old value of b
New value of b = Old value of a
#include<stdio.h>
int main() {
int a, b;
scanf( "%d%d" , &a, &b);
int tmp;
tmp = a;
a = b;
b = tmp;
return 0;
}
#include<stdio.h>
int main() {
int a, b;
scanf( "%d%d" , &a, &b);
int tmp;
tmp = a;
a = b;
b = tmp%b;
return 0;
}
13
3. (Medium) You find an old program you wrote to do the following:
New value of a = Old value of a/Old value of b
New value of b = Old value of b/Old value of a
#include<stdio.h>
int main() {
int a, b;
scanf( "%d%d" , &a, &b);
int tmp;
tmp = a;
a = a/b;
b = b/tmp;
return 0;
}
#include<stdio.h>
int main() {
int a, b, c;
scanf( "%d%d%d" , &a, &b, &c);
int tmp1, tmp2;
tmp1 = a;
a = (b+c)/2;
tmp2 = b;
b = c-(tmp1%tmp2);
c = tmp1*tmp2;
return 0;
}
14
5. (Very Hard) You find an old program you wrote to take 100 positive(greater than zero)
distinct numbers as input from the user and store them in an array arr. Then do some
operations such that:
New value of arr[i] = Old value of (arr[i])/(arr[0]+arr[1]+arr[2]· · ·arr[99]-arr[i])
#include<stdio.h>
int main() {
unsigned int arr[100], i;
for(i=0; i<100; i++) {
scanf( "%d" , &arr[i]);
}
unsigned int sum=0;
for(i=0; i<100; i++) {
sum += arr[i];
}
for(i=0; i<100; i++) {
arr[i] = (arr[i])/(sum-arr[i]);
}
return 0;
}
Rewrite the program without using sum. The only variables you’re allowed to use are
arr and i. Assume that for all arr[i], 0 < arr[i] < 100.
15
Hamming distance
The Hamming distance of two numbers of the same length is defined as the number of positions
at which the numbers differ.
For example, the numbers
1847
2849
have a Hamming distance of 2.
The numbers
4189241
4382384
have a Hamming distance of 5.
1. (Easy) Write a program to take two inputs from the user(assume the lengths to be equal).
Calculate and print their Hamming distance:
$./a.out $./a.out
134 3493284
235 4391283
2. (Medium) Write a program to take an input from the user(at least two digits, assume that
it fits in an int). Print the count of all numbers of the same length whose Hamming
distance with the input is 1:
$./a.out $./a.out
39 234
17 26
3. (Hard) Write a program to take an input from the user(at least two digits, assume that it
fits in an int). Print the sum of all numbers of the same length whose Hamming distance
with the input is 1:
$./a.out $./a.out
39 234
798 8939
16
4. (Very Hard) Write a program which prints each BINARY number from 0000000 to 1111111
exactly once with the condition that the Hamming distance of two consecutive numbers
should be 1.
For example, your output cannot contain
..
.
0000001
0000010
..
.
5. (Very Very Hard) Write a program which prints each decimal number from 0000000 to
9999999 exactly once with the condition that the Hamming distance of two consecutive
numbers should be 1.
For example, your output cannot contain
..
.
0000009
0000010
..
.
17
Balanced Parentheses
Let us define a Balanced Parenthesis String as follows:
1. A Balanced Parenthesis String is made up of only '(' and ')'
2. Every '(' in a Balanced Parenthesis String has a corresponding ')' which comes after it
For example,
(((())))
(()()(()))
()()()
(((())()((()()(())))))
are all Balanced Parenthesis Strings, while
())(())(
)()()()(
(()))()(()
are not.
1. (Medium) Write a program which takes a string as input and outputs whether it is a
Balanced Parenthesis String or not:
$./a.out $./a.out
(()())(()) (()))(()(())(
YES NO
Write a program which takes a string as input and outputs whether it is a Balanced
Bracket String or not:
$./a.out $./a.out
{}([]{}([]){}) {([()[({})]{}]})
YES NO
18
3. (Hard) Write a program which takes as input a number N and outputs the number of all
possible distinct Balanced Parenthesis Strings of length 2N. For example,
$./a.out $./a.out
3 10
5 16796
4. (Very Hard) Write a program which takes as input a number N and outputs all possible
Balanced Parenthesis Strings of length 2N. Each valid string should be printed exactly
once. For example,
$./a.out
$./a.out (((())))
((()()))
3 ((())())
((()))()
(()(()))
((()))
(()()())
(()())
(()())()
(())()
(())(())
()(())
(())()()
()()()
()((()))
()(()())
()(())()
()()(())
()()()()
19
5. (Very Very Hard) Write a program which takes three numbers X, Y, Z as input as outputs
all possible Balanced Bracket Strings with X pairs of curly braces, Y pairs of square
brackets and Z pairs of parentheses. Each valid string should be printed exactly once.
For example,
$./a.out
$./a.out 0 1 2
1 1 1
[(())]
[()()]
{[()]} [()]()
{[]()} [](())
{[]}() []()()
{([])} ([()])
{()[]} ([]())
{()}[] ([])()
{}[()] (([]))
{}[]() (()[])
{}([]) (())[]
{}()[] ()[()]
[{()}] ()[]()
[{}()] ()([])
[{}]() ()()[]
[({})]
[(){}] $./a.out | wc -l
[()]{}
[]{()} 1 2 3
[]{}()
[]({}) 7920
[](){}
({[]}) $./a.out | wc -l
({}[])
({})[] 2 2 2
([{}])
([]{}) 11880
([]){}
(){[]} $./a.out | wc -l
(){}[]
2 3 4
()[{}]
()[]{}
6126120
20
6. (Nearly Impossible) Let us define a ‘Balanced Bracket Quote String’, which apart from (,
), {, }, [ and ], includes single quotes(') as well, which should also be balanced and nested
properly. Opening and closing quotes will be represented by the same character. Thus, a
single quote can stand for either one depending on the context. Write a program which
takes four numbers X, Y, Z, Q as input and outputs all possible Balanced Bracket Quote
Strings with X pairs of curly braces, Y pairs of square brackets, Z pairs of parentheses and
Q pairs of single quotes. Each valid string should be printed exactly once. For example,
$./a.out
$./a.out
0 1 1 1
0 0 0 4
[( ' ' )]
[() ' ' ] ''''''''
[()] ' '
[ ' () ' ] $./a.out
[ ' ' ()]
[ ' ' ]() 0 0 1 2
[]( ' ' )
[]() ' ' ( ' ' ' ')
[] ' () ' ( ' ') ' '
[] ' ' () () ' ' ' '
([ ' ' ]) '( ' ') '
([] ' ' ) ' () ' ' '
([]) ' ' ' ' () ' '
( ' [] ' ) ' ' ' () '
( ' ' []) ' ' ' ' ()
( ' ' )[] ' '( ' ')
()[ ' ' ]
()[] ' ' $./a.out | wc -l
() ' [] '
() ' ' [] 1 1 1 1
' [()] '
' []() ' 336
' [] ' ()
' ([]) ' $./a.out | wc -l
' ()[] '
2 2 2 2
' () ' []
' ' [()]
2882880
' ' []()
' ' ([])
' ' ()[]
21