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:
Input: n = 5 Output: 2 Explanation: Because the 3rd row is incomplete, we return 2.
Example 2:
Input: n = 8 Output: 3 Explanation: Because the 4th row is incomplete, we return 3.
in the problem statement we understand the there given an integer n we need to find the insufficient coins index in the problem there is a box we all are know we start at index 1 to so on it is increasing the one by one we decreses the coins when index is increaing at certain index the coins go's negative value and that index we return
first we start the iteration from 1 to n and n is the coins we story into another variable int k = n; here k is decreases by index values updating the k values k = k - i ; after this we write the if condition if ( k < 0) then its true returning the index value by substracting the -1 because we all know index star with 0 not 1 so to satisfy this condition we write like this
CODE:
if you understand the logic of this problem solve this by click here
if you want the code for this problem click here
YOUTUBE:
0 Comments