Skip to content

Commit

Permalink
create 0441-arranging-coints.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Maunish-dave committed Jan 16, 2023
1 parent 713bfca commit 6d5c912
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cpp/0441-arranging-coins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int arrangeCoins(int n) {
int l=1,r = n,answer=1;
long long sum,m;

// binary search the values
while(l<=r){
m = l + (r-l)/2;

sum = m * (m+1)/2;

if(sum==n){
return (int)m;
}
else if(n<sum){
r = m -1;
}
else{
answer = (int)m;
l = m + 1;
}
}

return answer;
}
};

0 comments on commit 6d5c912

Please sign in to comment.