Given the total number of bricks T, find the number of stair steps that can be formed by using given bricks such that if step S has bricks B, then step S+1 should have exactly B+1 bricks and total number of bricks used should be less than or equal to number of bricks available.
Note: Number of bricks required to make step 1 of stair is 2, i.e step S = 1 must have exactly B = 2 bricks.
Input : 15 Output : 4 Bricks should be arranged in this pattern to solve for T = 15: Explanation: Number of bricks at step increases by one. At Step 1, Number of bricks = 2, Total = 2 At step 2, Number of bricks = 3, Total = 5 At step 3, Number of bricks = 4, Total = 9 At step 4, Number of bricks = 5, Total = 14 If we add 6 more bricks to form new step, then the total number of bricks available will surpass. Hence, number of steps that can be formed are 4 and number of bricks used are 14 and we are left with 1 brick which is useless. Input : 40 Output : 7 Bricks should be arranged in this pattern to solve for T = 40: Explanation: At Step 1, Number of bricks = 2, Total = 2 At step 2, Number of bricks = 3, Total = 5 At step 3, Number of bricks = 4, Total = 9 At step 4, Number of bricks = 5, Total = 14 At step 5, Number of bricks = 6, Total = 20 At step 6, Number of bricks = 7, Total = 27 At step 7, Number of bricks = 8, Total = 35 If we add 9 more bricks to form new step, then the total number of bricks available will surpass. Hence, number of steps that can be formed are 7 and number of bricks used are 35 and we are left with 5 bricks which are useless.
We are interested in the number of steps and we know that each step Si uses exactly Bi number of bricks. We can represent this problem as an equation:
n * (n + 1) / 2 = T (For Natural number series starting from 1, 2, 3, 4, 5 …)
n * (n + 1) = 2 * T
n-1 will represent our final solution because our series in problem starts from 2, 3, 4, 5…
Now, we just have to solve this equation and for that we can exploit binary search to find the solution to this equation. Lower and Higher bounds of binary search are 1 and T.
Below is the implementation for the above approach:
Number of stair steps = 4
This article is contributed by Abhishek rajput.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Find the number of ways to reach Kth step in stair case
- Count ways to reach the n'th stair
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Find the minimum number of steps to reach M from N
- Find the Alpha Score of the Given Steps (Using BST)
- Number of steps to convert to prime factors
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Puzzle | Minimum number steps to weigh 1 kg rice with 1gm weight
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Largest number N which can be reduced to 0 in K steps
- Print steps to make a number in form of 2^X - 1
- Minimize the number of steps required to reach the end of the array | Set 2
- Number of steps to sort the array by changing order of three elements in each step
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Minimum number of steps required to obtain the given Array by the given operations
- Count minimum steps to get the given desired array
- Minimum steps to come back to starting point in a circular tour
- Probability of reaching a point with 2 or 3 steps at a time
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 : jit_t