# Find the number of stair steps

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.

Examples:

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.

**Approach:**

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:

## C++

`// CPP program to find the number of steps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Modified Binary search function ` `// to solve the equation ` `int` `solve(` `int` `low, ` `int` `high, ` `int` `T) ` `{ ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) / 2; ` ` ` ` ` `// if mid is solution to equation ` ` ` `if` `((mid * (mid + 1)) == T) ` ` ` `return` `mid; ` ` ` ` ` `// if our solution to equation ` ` ` `// lies between mid and mid-1 ` ` ` `if` `(mid > 0 && (mid * (mid + 1)) > T && ` ` ` `(mid * (mid - 1)) <= T) ` ` ` `return` `mid - 1; ` ` ` ` ` `// if solution to equation is ` ` ` `// greater than mid ` ` ` `if` `((mid * (mid + 1)) > T) ` ` ` `high = mid - 1; ` ` ` ` ` `// if solution to equation is less ` ` ` `// than mid ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `T = 15; ` ` ` ` ` `// call binary search method to ` ` ` `// solve for limits 1 to T ` ` ` `int` `ans = solve(1, T, 2 * T); ` ` ` ` ` `// Because our pattern starts from 2, 3, 4, 5... ` ` ` `// so, we subtract 1 from ans ` ` ` `if` `(ans != -1) ` ` ` `ans--; ` ` ` ` ` `cout << ` `"Number of stair steps = "` ` ` `<< ans << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of steps ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Modified Binary search function ` ` ` `// to solve the equation ` ` ` `public` `static` `int` `solve(` `int` `low, ` `int` `high, ` `int` `T) ` ` ` `{ ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) / ` `2` `; ` ` ` ` ` `// if mid is solution to equation ` ` ` `if` `((mid * (mid + ` `1` `)) == T) ` ` ` `return` `mid; ` ` ` ` ` `// if our solution to equation ` ` ` `// lies between mid and mid-1 ` ` ` `if` `(mid > ` `0` `&& (mid * (mid + ` `1` `)) > T && ` ` ` `(mid * (mid - ` `1` `)) <= T) ` ` ` `return` `mid - ` `1` `; ` ` ` ` ` `// if solution to equation is ` ` ` `// greater than mid ` ` ` `if` `((mid * (mid + ` `1` `)) > T) ` ` ` `high = mid - ` `1` `; ` ` ` ` ` `// if solution to equation is less ` ` ` `// than mid ` ` ` `else` ` ` `low = mid + ` `1` `; ` ` ` `} ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `main(String argc[]) ` ` ` `{ ` ` ` `int` `T = ` `15` `; ` ` ` ` ` `// call binary search method to ` ` ` `// solve for limits 1 to T ` ` ` `int` `ans = solve(` `1` `, T, ` `2` `* T); ` ` ` ` ` `// Because our pattern starts from 2, 3, 4, 5... ` ` ` `// so, we subtract 1 from ans ` ` ` `if` `(ans != -` `1` `) ` ` ` `ans--; ` ` ` ` ` `System.out.println(` `"Number of stair steps = "` `+ ans); ` ` ` `} ` `} ` ` ` `/* This code is Contributed by Sagar Shukla */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the number of steps ` ` ` `# Modified Binary search function ` `# to solve the equation ` `def` `solve( low, high, T ): ` ` ` ` ` `while` `low <` `=` `high: ` ` ` `mid ` `=` `int` `((low ` `+` `high) ` `/` `2` `) ` ` ` ` ` `# if mid is solution to equation ` ` ` `if` `(mid ` `*` `(mid ` `+` `1` `)) ` `=` `=` `T: ` ` ` `return` `mid ` ` ` ` ` `# if our solution to equation ` ` ` `# lies between mid and mid-1 ` ` ` `if` `(mid > ` `0` `and` `(mid ` `*` `(mid ` `+` `1` `)) > T ` ` ` `and` `(mid ` `*` `(mid ` `-` `1` `)) <` `=` `T) : ` ` ` `return` `mid ` `-` `1` ` ` ` ` `# if solution to equation is ` ` ` `# greater than mid ` ` ` `if` `(mid ` `*` `(mid ` `+` `1` `)) > T: ` ` ` `high ` `=` `mid ` `-` `1` `; ` ` ` ` ` `# if solution to equation is ` ` ` `# less than mid ` ` ` `else` `: ` ` ` `low ` `=` `mid ` `+` `1` ` ` `return` `-` `1` ` ` `# driver code ` `T ` `=` `15` ` ` `# call binary search method to ` `# solve for limits 1 to T ` `ans ` `=` `solve(` `1` `, T, ` `2` `*` `T) ` ` ` `# Because our pattern starts from 2, 3, 4, 5... ` `# so, we subtract 1 from ans ` `if` `ans !` `=` `-` `1` `: ` ` ` `ans` `-` `=` `1` ` ` `print` `(` `"Number of stair steps = "` `, ans) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of steps ` `using` `System; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Modified Binary search function ` ` ` `// to solve the equation ` ` ` `public` `static` `int` `solve(` `int` `low, ` `int` `high, ` `int` `T) ` ` ` `{ ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) / 2; ` ` ` ` ` `// if mid is solution to equation ` ` ` `if` `((mid * (mid + 1)) == T) ` ` ` `return` `mid; ` ` ` ` ` `// if our solution to equation ` ` ` `// lies between mid and mid-1 ` ` ` `if` `(mid > 0 && (mid * (mid + 1)) > T && ` ` ` `(mid * (mid - 1)) <= T) ` ` ` `return` `mid - 1; ` ` ` ` ` `// if solution to equation is ` ` ` `// greater than mid ` ` ` `if` `((mid * (mid + 1)) > T) ` ` ` `high = mid - 1; ` ` ` ` ` `// if solution to equation is less ` ` ` `// than mid ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `T = 15; ` ` ` ` ` `// call binary search method to ` ` ` `// solve for limits 1 to T ` ` ` `int` `ans = solve(1, T, 2 * T); ` ` ` ` ` `// Because our pattern starts ` ` ` `//from 2, 3, 4, 5... ` ` ` `// so, we subtract 1 from ans ` ` ` `if` `(ans != -1) ` ` ` `ans--; ` ` ` `Console.WriteLine(` `"Number of stair steps = "` `+ ans); ` ` ` `} ` `} ` ` ` `/* This code is Contributed by vt_m */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// number of steps ` ` ` `// Modified Binary search function ` `// to solve the equation ` `function` `solve(` `$low` `, ` `$high` `, ` `$T` `) ` `{ ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` `$mid` `= (` `$low` `+ ` `$high` `) / 2; ` ` ` ` ` `// if mid is solution ` ` ` `// to equation ` ` ` `if` `((` `$mid` `* (` `$mid` `+ 1)) == ` `$T` `) ` ` ` `return` `$mid` `; ` ` ` ` ` `// if our solution to equation ` ` ` `// lies between mid and mid-1 ` ` ` `if` `(` `$mid` `> 0 && (` `$mid` `* (` `$mid` `+ 1)) > ` `$T` `&& ` ` ` `(` `$mid` `* (` `$mid` `- 1)) <= ` `$T` `) ` ` ` `return` `$mid` `- 1; ` ` ` ` ` `// if solution to equation is ` ` ` `// greater than mid ` ` ` `if` `((` `$mid` `* (` `$mid` `+ 1)) > ` `$T` `) ` ` ` `$high` `= ` `$mid` `- 1; ` ` ` ` ` `// if solution to ` ` ` `// equation is less ` ` ` `// than mid ` ` ` `else` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$T` `= 15; ` ` ` ` ` `// call binary search ` ` ` `// method to solve ` ` ` `// for limits 1 to T ` ` ` `$ans` `= solve(1, ` `$T` `, 2 * ` `$T` `); ` ` ` ` ` `// Because our pattern ` ` ` `// starts from 2, 3, 4, 5... ` ` ` `// so, we subtract 1 from ans ` ` ` `if` `(` `$ans` `!= -1) ` ` ` `$ans` `--; ` ` ` ` ` `echo` `"Number of stair steps = "` `, ` `$ans` `, ` `"\n"` `; ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

Output:

Number of stair steps = 4

This article is contributed by **Abhishek rajput**.

## Recommended Posts:

- 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
- Print steps to make a number in form of 2^X - 1
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- 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
- Given number of matches played, find number of teams in tournament
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the smallest number whose digits multiply to a given number n

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.