Square Patio Tiles
You want to lay a pile of identical square tiles as one square patio, as large as possible. A side of 4 needs 4 × 4 = 16 tiles. Any tiles that do not fit in the square go back in the shed.
Given the number of tiles, you need to find the longest side the patio can have.
Your task
Write largestSide(tiles). Return the largest whole number k with k × k ≤ tiles. With 0 tiles the answer is 0.
Do not use a built-in square root or power function. Find k by searching.
largestSide(tiles: int) → int
Examples
tiles = 17
4
A side of 4 uses 16 tiles, and 1 is left over. A side of 5 would need 25, which you do not have, so the answer is 4.
tiles = 36
6
6 × 6 = 36 exactly, so every tile is used.
Constraints
- 0 ≤ tiles ≤ 2,000,000,000
Start with brute force
Try sides 1, 2, 3, … until the square exceeds tiles. The answer is one less.
That is about √tiles steps: 44,721 for 2,000,000,000 tiles. Binary search needs about 31.
Hints
0 of 4 shown- Hint 1
Mark sides 0, 1, 2, 3, 4, 5, 6 as "fits" or "too big" for 17 tiles. What pattern do the marks make?
- Hint 2
Every side up to some point fits, and every side after that point is too big. The answer is the last side that fits.
- Hint 3
Binary search over sides 0 to
tiles. The check for a middle side ismid * mid <= tiles. If it fits, the answer ismidor larger; if not, smaller. - Hint 4
Keep
lo = 0,hi = tiles. Whilelo < hi, take the upper middle,mid = floor((lo + hi + 1) / 2). If it fits,lo = mid; elsehi = mid - 1. The upper middle stops the loop getting stuck whenhi = lo + 1.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
"Does side k fit?" is yes for small k and no for large k, so the answers form a sorted row of yes values followed by no values. Binary search finds the last yes.
Trace 17 tiles:
- With
lo = 0andhi = 17,mid = 9. Since 81 > 17, the side is too big, sohi = 8. - With
lo = 0andhi = 8,mid = 4. Since 16 ≤ 17, the side fits, solo = 4. - With
lo = 4andhi = 8,mid = 6. Since 36 > 17, the side is too big, sohi = 5. - With
lo = 4andhi = 5,mid = 5. Since 25 > 17, the side is too big, sohi = 4. - Now
lo = hi = 4, so the loop ends and the function returns 4.
In step 4 the lower middle would be 4. That side fits, so lo would stay 4 and the loop would never end. When the "yes" branch sets lo = mid, round the middle up.
With 32-bit integers, mid * mid can overflow, so compare mid <= tiles / mid instead. JavaScript and Python numbers are big enough for this range.
function largestSide(tiles) {
let lo = 0, hi = tiles; // the answer is in [lo, hi]
while (lo < hi) {
const mid = Math.floor((lo + hi + 1) / 2); // upper middle
if (mid * mid <= tiles) lo = mid; // mid fits: answer is mid or bigger
else hi = mid - 1; // too big: answer is smaller
}
return lo;
}def largest_side(tiles):
lo, hi = 0, tiles # the answer is in [lo, hi]
while lo < hi:
mid = (lo + hi + 1) // 2 # upper middle
if mid * mid <= tiles:
lo = mid # mid fits: answer is mid or bigger
else:
hi = mid - 1 # too big: answer is smaller
return loRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.