Concept:
1+2+3+...+x = n
-> (1+x)x/2 = n
-> x^2+x = 2n
-> x^2+x+1/4 = 2n +1/4
-> (x+1/2)^2 = 2n +1/4
-> (x+0.5) = sqrt(2n+0.25)
-> x = -0.5 + sqrt(2n+0.25)
int arrangeCoins(int n) {
return floor(-0.5+sqrt((double)2*n+0.25));
}
@zmcx16 Why cannot I use the normal expression to find the root ?
ax^2+bx+c=0
-b+sqrt(b^2 - 4ac)/2*a
@sujiths52 Use -b+sqrt(b^2 - 4ac)/2*a is correct too.
I just found my solution intuition.