Skip to content

Instantly share code, notes, and snippets.

@KeshariPiyush24
Created September 21, 2024 05:34
Show Gist options
  • Save KeshariPiyush24/5c1916afa9568fab262b11ac5e48dfcb to your computer and use it in GitHub Desktop.
Save KeshariPiyush24/5c1916afa9568fab262b11ac5e48dfcb to your computer and use it in GitHub Desktop.
441. Arranging Coins

Question: 441. Arranging Coins

Intution:

Time Complexity: $$O(log(n))$$

Space Complexity: $$O(1)$$

Solution:

class Solution {
    public int arrangeCoins(int n) {
        long low = 1;
        long high = n;
        while (low <= high) {
            long mid = low + (high - low) / 2;
            if (2l * n == mid * (mid + 1)) return (int) mid;
            else if (2l * n < mid * (mid + 1)) high = mid - 1;
            else low = mid + 1;
        }
        return (int) high;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment