Skip to content

Commit 7dc41b2

Browse files
authored
Merge pull request #2051 from Maunish-dave/0441-arranging-coins
Create 0441-arranging-coints.cpp
2 parents 713bfca + 6d5c912 commit 7dc41b2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cpp/0441-arranging-coins.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)