441. Arranging Coins (Leetcode || Java || Easy)

You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.

Given the integer n, return the number of complete rows of the staircase you will build.

Example 1:

Example 2:

Constraints:

  • 1 <= n <= 231 - 1

SOLUTION:

class Solution {
public int arrangeCoins(int n) {
int count = 0;//counting number of steps n will cover
int i = 1;

while(n > 0){
n = n — i;//eliminating steps

i++;
count++;

if(n == 0){//if n becomes 0 it means the last step is also filled
return count;
}
}

return count — 1;//n do no become zero in the last hence, we do not consider the last step
}
}

Thank you for reading. If you have any queries, please let me know int the comment section.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Palakkgoyal

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.