KatInterview

441. Arranging Coins

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the maximum number of complete rows, k, of a staircase that can be built using n coins. The i-th row of the staircase has i coins. Therefore, to build k complete rows, we need a total of 1 + 2 + 3 + ... + k coins. This sum is an arithmetic series which can be calculated using the formula k * (k + 1) / 2.

Solution

Arranging Coins in Python

class Solution:
    def arrangeCoins(self, n: int) -> int:
        # We are looking for the largest k such that k * (k + 1) / 2 <= n.
        # This can be solved using binary search.
        # The search space for k is from 1 to n.
        left, right = 1, n
        k = 0 # To store the result

        while left <= right:
            mid = left + (right - left) // 2
            # Python integers handle arbitrary size, so overflow is not an issue.
            coins = mid * (mid + 1) // 2
            
            if coins == n:
                return mid
            
            if coins < n:
                # We can form mid rows. Try for more.
                # Store mid as a potential answer and search in the right half.
                k = mid
                left = mid + 1
            else:
                # Not enough coins for mid rows. Search in the left half.
                right = mid - 1
        
        return k

Arranging Coins in Java

class Solution {
    public int arrangeCoins(int n) {
        // We are looking for the largest k such that k * (k + 1) / 2 <= n.
        // This is a monotonic function, so we can use binary search.
        // Search space for k is [1, n].
        long left = 1, right = n;
        long k = 0; // To store the result

        while (left <= right) {
            long mid = left + (right - left) / 2;
            // Calculate coins needed for mid rows. Use long to prevent overflow.
            long coins = mid * (mid + 1) / 2;

            if (coins == n) {
                // Exactly n coins are used for mid rows. This is the answer.
                return (int)mid;
            }

            if (coins < n) {
                // We can form mid rows. Let's see if we can form more.
                // Store mid as a potential answer and search in the right half.
                k = mid;
                left = mid + 1;
            } else {
                // Not enough coins for mid rows. Search in the left half.
                right = mid - 1;
            }
        }
        
        // After the loop, k holds the maximum number of complete rows.
        return (int)k;
    }
}

Arranging Coins in C++

class Solution {
public:
    int arrangeCoins(int n) {
        // We are looking for the largest k such that k * (k + 1) / 2 <= n.
        // This can be solved using binary search.
        // The search space for k is from 1 to n.
        long left = 1, right = n;
        long k = 0; // To store the result
        
        while (left <= right) {
            // Use long for mid to avoid overflow in the coins calculation.
            long mid = left + (right - left) / 2;
            long coins = mid * (mid + 1) / 2;
            
            if (coins == n) {
                // Exactly n coins are used for mid rows. This is the answer.
                return (int)mid;
            }
            
            if (coins < n) {
                // We can form mid rows. Let's see if we can form more.
                // Store mid as a potential answer and search in the right half.
                k = mid;
                left = mid + 1;
            } else {
                // Not enough coins for mid rows. Search in the left half.
                right = mid - 1;
            }
        }
        
        // After the loop, k holds the maximum number of complete rows.
        return (int)k;
    }
};

Arranging Coins in TypeScript

function arrangeCoins(n: number): number {
    // We are looking for the largest k such that k * (k + 1) / 2 <= n.
    // This can be solved using binary search.
    // The search space for k is from 1 to n.
    let left = 1, right = n;
    let k = 0; // To store the result

    while (left <= right) {
        // Standard number type in JS/TS is a 64-bit float.
        // `mid * (mid + 1)` could exceed Number.MAX_SAFE_INTEGER (2^53 - 1)
        // if n is large, leading to precision loss. 
        // Using BigInt for calculations guarantees precision.
        const mid = left + Math.floor((right - left) / 2);
        
        const midBig = BigInt(mid);
        const coins = midBig * (midBig + 1n) / 2n;
        const nBig = BigInt(n);

        if (coins === nBig) {
            return mid;
        }
        
        if (coins < nBig) {
            // We can form mid rows. Try for more.
            k = mid;
            left = mid + 1;
        } else {
            // Not enough coins. Try fewer rows.
            right = mid - 1;
        }
    }
    
    return k;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.