Given a list of integer each representing the length of each stick and an integer which tells how many times we can break a stick into half parts, we have to find maximum desired length sticks can be obtained from the group of sticks.
Note 1: When we break a stick it gets converted into two half parts for example for a stick of length 10 two sticks can be obtained of both 5 in length and for a stick of length 5 two sticks will be obtained of length 2 and 3 respectively.
Note 2: Discarded part can’t be used again for making sticks such that if a stick of length 11 is given we can break it into 5 and 6 of length pieces then we have to discard one of the pieces which can’t be used further.
list = [2, 3, 4, 11]
n = 2
desired_length = 3
Maximum sticks of desired length that can be obtained are : 3
We already have one stick of length 3 and two more sticks can be obtained
by breaking stick of length 11 into [5, 3, 3] pieces therefore total sticks will be 3.
list = [2, 1, 4, 5]
n = 2
desired_length = 4
Maximum sticks of desired length that can be obtained are : 1
We already have one stick of length 4 and no more sticks can be obtained
by breaking any stick therefore total sticks will be 1
To solve the problem mentioned above we will first do a linear search operation to find all the sticks which have exact same length as of the desired stick length and count them. We will then store the count in the variable. Obviously we have to discard all the sticks which have a length less than the desired length as with them we can’t make any desired length stick. Then pass the value of sticks that have length more than the desired length to a function which calculate how many sticks can be obtained by breaking them. With the help of recursion find number of ways in which sticks can be obtained.
Below is the implementation of the above approach:
- Find the sum of remaining sticks after each iterations
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Maximum sum after repeatedly dividing N by a divisor
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Python | Dividing two lists
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Length of longest increasing index dividing subsequence
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Largest number dividing maximum number of elements in the array
- Josephus problem | Set 1 (A O(n) Solution)
- Shortest Superstring Problem | Set 2 (Using Set Cover)
- Competitive Programming: Conquering a given problem
- Josephus problem | Set 2 (A Simple Solution when k = 2)
- Word Break Problem using Backtracking
- Word Break Problem | DP-32 | Set - 2
- Exact Cover Problem and Algorithm X | Set 1
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Maths behind number of paths in matrix problem
- Water Connection Problem
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Stream_Cipher