Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2051 from Maunish-dave/0441-arranging-…
Browse files Browse the repository at this point in the history
…coins

Create 0441-arranging-coints.cpp
  • Loading branch information
MHamiid authored Jan 16, 2023
2 parents 713bfca + 6d5c912 commit 7dc41b2
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 7dc41b2

Please sign in to comment.