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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Find the number of ways to reach Kth step in stair case
- Find the minimum number of steps to reach M from N
- Largest number N which can be reduced to 0 in K steps
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Print steps to make a number in form of 2^X - 1
- Number of steps to convert to prime factors
- Minimize the number of steps required to reach the end of the array | Set 2
- Puzzle | Minimum number steps to weigh 1 kg rice with 1gm weight
- Count ways to reach the n'th stair
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Find minimum number to be divided to make a number a perfect square
- Find the largest number smaller than integer N with maximum number of set bits
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