Home » Java » java – Maximum height of the staircase-Exceptionshub

# java – Maximum height of the staircase-Exceptionshub

Questions:

Given an integer A representing the square blocks. The height of each square block is 1. The task is to create a staircase of max height using these blocks. The first stair would require only one block, the second stair would require two blocks and so on. Find and return the maximum height of the staircase.

Your submission failed for the following input: A : 92761

Your function returned the following : 65536
The expected returned value : 430

``````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.
``````

CODE

``````public int solve(int A) {
int l=1,h=A,T=2*A;
while(l<=h)
{
int mid=l+(h-l)/2;
if((mid*(mid+1))==T)
return mid;
if((mid*(mid+1))>T && (mid!=0 && (mid*(mid-1))<=T) )
return mid-1;
if((mid*(mid+1))>T)
h=mid-1;
else
l=mid+1;
}
return 0;
}
``````

To expand on the comment by Matt Timmermans:

You know that for `n` steps, you need `(n * (n + 1))/2` blocks. You want know, if given `B` blocks, how many steps you can create.

So you have:

``````(n * (n + 1))/2 = B
(n^2 + n)/2 = B
n^2 + n = 2B
n^2 + n - 2B = 0
``````

That looks suspiciously like something for which you’d use the quadratic formula.

In this case, `a=1`, `b=1`, and `c=(-2B)`. Plugging the numbers into the formula:

``````n = ((-b) + sqrt(b^2 - 4*a*c))/(2*a)
= (-1 + sqrt(1 - 4*1*(-2B)))/(2*a)
= (-1 + sqrt(1 + 8B))/2
= (sqrt(1 + 8B) - 1)/2
``````

So if you have 5050 blocks, you get:

``````n = (sqrt(1 + 40400) - 1)/2
= (sqrt(40401) - 1)/2
= (201 - 1)/2
= 100
``````

Try it with the quadratic formula calculator. Use 1 for the value of a and b, and replace c with negative two times the number of blocks you’re given. So in the example above, c would be -10100.

In your program, since you can’t have a partial step, you’d want to truncate the result.

Why are you using all these formulas? A simple `while()` loop should do the trick, eventually, it’s just a simple Gaussian Sum ..

``````public static int calculateStairs(int blocks) {
int lastHeight = 0;
int sum = 0;
int currentHeight = 0; //number of bricks / level
while (sum <= blocks) {
lastHeight = currentHeight;
currentHeight++;
sum += currentHeight;
}
return lastHeight;
}
``````

So this should do the job as it also returns the expected value. Correct me if im wrong.

``````public int solve(int blocks) {
int current; //Create Variables
for (int x = 0; x < Integer.MAX_VALUE; x++) { //Increment until return
current = 0; //Set current to 0

//Implementation of the Gauss sum
for (int i = 1; i <= x; i++) { //Sum up [1,*current height*]
current += i;
} //Now we have the amount of blocks required for the current height

//Now we check if the amount of blocks is bigger than
// the wanted amount, and if so we return the last one
if (current > blocks) {
return x - 1;
}
}
return current;
}
``````