Skip to content

Instantly share code, notes, and snippets.

@riyafa
Created March 15, 2022 07:33
Show Gist options
  • Save riyafa/e2d60885d54708099e8b4ba3d95bb017 to your computer and use it in GitHub Desktop.
Save riyafa/e2d60885d54708099e8b4ba3d95bb017 to your computer and use it in GitHub Desktop.
Binary search solution for 441. Arranging Coins: https://youtu.be/H-Tdu8qJ_uk
class ArrangingCoinsBS {
public int arrangeCoins(int n) {
int low = 1;
int high = n;
while(low < high) {
int mid = low +(high -low+1)/2;
long num = (((long)mid * ((long)mid +1l))/2l);
if(num == n) {
return mid;
} else if(n < num){
high = mid-1;
} else {
low = mid;
}
}
return low;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment