0% found this document useful (0 votes)
159 views

Recursion Practice Problems C++

The document contains 4 practice problems on recursion: 1) Drawing Stark's triangle of a given depth using recursion. 2) Finding the maximum number of chocolates that can be obtained given an initial money amount, chocolate price, and wrapper exchange rule using recursion. 3) Finding the minimum number of steps to reach a destination point on a grid from a source point using only specific move rules and recursion. 4) Performing prime factorization of a given number recursively by repeatedly dividing by the smallest prime factor.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Recursion Practice Problems C++

The document contains 4 practice problems on recursion: 1) Drawing Stark's triangle of a given depth using recursion. 2) Finding the maximum number of chocolates that can be obtained given an initial money amount, chocolate price, and wrapper exchange rule using recursion. 3) Finding the minimum number of steps to reach a destination point on a grid from a source point using only specific move rules and recursion. 4) Performing prime factorization of a given number recursively by repeatedly dividing by the smallest prime factor.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Note: These are some practice problems on recursion taken from CS101 - Fall 2017.

Problem 1 : ​ Stark’s Triangle

Stark’s triangle is a unique figure with overall shape of an equilateral triangle, subdivided
recursively into smaller equilateral triangles. The length of the outside triangle remains
fixed, equal to 400 pixels. You have to take an integer n as an input and draw the stark
triangle of that depth.

Input format:
A number n which signifies the depth of Stark’s triangle. (1<n<10)

Output format:
The equivalent Stark’s triangle using turtleSim.
Problem 2: ​Chocolate and Wrappers

Description:

Cadbury has started a new promotional offer to attract customers. You can get a chocolate
for free by returning exactly ​n wrappers​ to the shopkeeper. Assume that the initial money
you have is​ m units ​and cost of each chocolate is ​p units​, find the maximum number of
chocolates you can buy/eat.
Assume each chocolate is enclosed in a wrapper and you don’t discard any wrapper of the
chocolate(s) you ate. Also you can go to the shopkeeper any number of times. Assume you
have 0 wrappers initially and the shopkeeper gives you a chocolate if you return exactly n
wrappers. ​You must use recursion to solve this.

Solution Strategy:

You are required to write two functions ​countMaxChoco() and countMaxWrap()​.


The description for the two functions to be written are given below.

countMaxWrap(r,n):

Given ​r​ wrappers and 1 chocolate per ​n​ wrappers, find the maximum chocolates you can
get.
To implement this functionality assume you can get k1 chocolates from k1*n wrappers, now
you have r-k1*n(old) + k1(new) wrappers and so we can write
Maximum chocolates obtained from r wrappers
= k1 + Maximum chocolates obtained from ( r-k1*n+k1) wrappers.

This above equality is clearly a recursion.


Use the appropriate k1 value during recursion and implement ​countMaxWrap​(r,n)

countMaxChoco(m,p,n):

You can calculate maximum number of chocolates you can buy from the money.You get the
initial number of wrappers available. Use ​number​ function to get the maximum no of
chocolates you can get from the wrappers.
Total Chocolates = Chocolates from money + Chocolates from wrappers
Return the appropriate total count from the function.

The main program calls ​countMaxChoco​() with appropriate arguments.


Examples:

Input :

m = 16, p = 2, n = 2

Output : ​ 15
Price of a chocolate is 2.

You can buy ​8​ chocolates from amount 16.(8 wrappers now)

You can return 8 wrappers back and get ​4​ more chocolates.(4 wrappers now)

Then you can return 4 wrappers and get ​2 ​more chocolates.(2 wrappers now)

Finally you can return 2 wrappers to get ​1​ more chocolate.(1 wrappers now)

With 1 wrapper you can’t buy any extra chocolates.

Total possible = 8 + 4 + 2 + 1 = 15 chocolates

Input : ​ m = 15, p = 1, n = 3
Output : ​ 22

Price of a chocolate is 1
We buy and eat ​15​ chocolates(15 wrappers now)
We return 15 wrappers and get ​5​ more chocolates.(5 wrappers now)
We return 3 wrappers, get ​1​ chocolate and eat it.(1 wrapper now + 2 old wrappers)
Now we have 3 wrappers. Return 3 and get ​1​ more chocolate.(1 wrapper now)

With 1 wrapper you can’t buy any extra chocolates.


So total chocolates = 15 + 5 + 1 + 1 = 22
Problem 3: ​Can I reach you?

Description​:

You are standing at a point on a grid. On any point (x,y) on the grid you are only allowed to
move to 2 points either (x,x+y) or (x+y,y).
Given coordinates of a ​source point (x1, y1) determine the number of steps required to
reach the ​destination point (x2, y2)​. Write a recursive function ​isReachable(x1,y1,x2,y2) to
achieve your functionality.The function should return the number of steps in which you can
reach the destination point. ​If there exists no path, return -1. You must use recursion to
solve this.
Note:
1. All coordinates are positive.
2. Assume if source point is equal to destination point, then the number of steps
required is 0.
3. If there are two or more paths possible, return the minimum of all path-steps. For
example if there are two paths from source to destination, path1 takes 3 steps and
path2 takes 5 steps, return 3 as the answer.

Solution Strategy:
If you are trying to go from (x1,y1) to (x2,y2) , then in the first step you either move to
(x1+y1,y1) or (x1,x1+y1).
If you go to (x2,y2) via (x1+y1,y1) then
Number of steps from (x1,y1) to (x2,y2) via (x1+y1,y1) is either
1) 1 + No of steps from (x1+y1,y1) to (x2,y2) // if valid path exists from (x1+y1,y1) to
(x2,y2)
2) -1 // If no path exist from (x1+y1,y1) to (x2,y2)
Similarly calculate Number of steps from (x1,y1) to (x2,y2) via (x1,x1+y1) ,p
If Number of steps from both the paths is non-negative, return the minimum value.
Else if one of the values is non-negative , return the appropriate number of steps.
Else if there exist no path in both cases, return -1
Make sure to write the terminating case for recursion correct. i.e
1) When source point and destination point are equal, Number of steps required = 0
2) There exists no path when x-coordinate of source point > x-coordinate of destination
point or y-coordinate of source point > y-coordinate of destination point as all the
coordinates are positive and you can’t reach a point whose coordinate is less than
source coordinate. Hence return -1 in this case
Examples:

Input :
(x1, y1) = (2, 10)
(x2, y2) = (26, 12)

Output :
3

Explanation:
(2, 10) -> (2, 2+10)=(2,12) -> (2+12, 12)=(14,12) -> (14+12,12) = (26, 12)
Valid path exists.

Input :
(x1, y1) = (20, 10)
(x2, y2) = (6, 12)
Output :
-1

Explanation:
No such path is possible because x1 > x2 and coordinates are positive
Problem 4: ​Factorize me!

Description:
Given an integer ​n​, your task is to perform prime factorization. Starting from the smallest
prime number you need to print its prime factors in increasing order the number of times
they divide n. Write a recursive function ​factorize(n)​ to solve this problem.You don’t need
any extra function(s) like isPrime(n) to solve this problem.

For example if n = 120, the prime factors are 2,3 and 5 (120 = 2*2*2*3*5)
The output should be 2 2 2 3 5

Solution Strategy:
For a particular n, find its least factor p > 1 (which will turn out to be prime, this need not be
checked due to construction of solution strategy in such a way)
Print p and then factorize n/p by calling ​factorize(n/p)​. Handle the termination case
appropriately.
This solution strategy works accurately because
1)The least factor greater than 1 of a number is definitely prime.
2)This strategy prints the output in required order (smallest prime factor first)

Examples:

Input:
120

Output:
22235

Explanation:
120/2 = 60
60/2 = 30
30/2 = 15
15/3 = 5
5/5 = 1

Input:
32
Output:
22222

You might also like