Skip to main content
Back to problems
#441
Easy Algorithms

Arranging coins

Math Binary Search
48.0% acceptance
Jan 13, 2026
4320
1375
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. Given the integer n, return the number of complete rows of the staircase you will build.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn arrange_coins(n: i32) -> i32 {
    let n = n as i64;
    let mut left = 0i64;
    let mut right = n;
    
    while left <= right {
      let mid = left + (right - left) / 2;
      let sum = mid * (mid + 1) / 2;
      
      if sum == n {
        return mid as i32;
      } else if sum < n {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
    
    right as i32
  }
}