We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 713bfca commit 6d5c912Copy full SHA for 6d5c912
cpp/0441-arranging-coins.cpp
@@ -0,0 +1,27 @@
1
+class Solution {
2
+public:
3
+ int arrangeCoins(int n) {
4
+ int l=1,r = n,answer=1;
5
+ long long sum,m;
6
+
7
+ // binary search the values
8
+ while(l<=r){
9
+ m = l + (r-l)/2;
10
11
+ sum = m * (m+1)/2;
12
13
+ if(sum==n){
14
+ return (int)m;
15
+ }
16
+ else if(n<sum){
17
+ r = m -1;
18
19
+ else{
20
+ answer = (int)m;
21
+ l = m + 1;
22
23
24
25
+ return answer;
26
27
+};
0 commit comments