In: Computer Science
You have been asked to create a program that builds a tower base on the number of items given as input.
When the only 1 item is given you create no tower you just place the item on top of the stand.
When 2 items are given you build the base of the tower with one foot to the right and one foot to the left.
The feet will be in the ground and support the tower.
For every additional item you must keep the tower balanced by placing half on the right and half on the left each side
What is the height of the tower for the number 5? _________
What is the height of the tower for the number 10? ________
Write the a function to calculate the height. _______
If this was coded recursively what would be the base case? _________
If this was done recursively what would be the Time Complexity? _____
Answer :
What is the height of the tower for the number 5?
Height is 2. Make a base with 3 items and place two item on them. Like shown in figure.
What is the height of the tower for the number 10?
Height is 4.
Write the a function to calculate the height.
C++ function :
int height(int n, int x=1)
{
if(n < x)
return 0;
return 1 + height(n-x, ++x);
}
call using height(n);
If this was coded recursively what would be the base case?
It should check the remaining item can make new base layer of size x or not.
If this was done recursively what would be the Time Complexity?
Time complexity : O(logn)
Hope you like it
Any Query? Comment Down!
I have written for you, Please up vote the answer as it encourage us to serve you Best !