Can Place Flowers

Can Place Flowers is a friendly greedy question. The rule is simple. No two flowers can sit next to each other. The interviewer wants to see if you can make the right local choice at each spot and handle the edges of the array cleanly. Those edges are where most people slip.

🎯 The Problem

You get a flowerbed and a number n. Decide if you can plant n more flowers with no two touching.

  • Each box is a plot. A 0 is empty. A 1 already has a flower.
  • You must plant n more flowers.
  • No two flowers may sit in plots that touch.
  • Return true if all n fit. Otherwise false.
  • A greedy choice grabs a spot the moment it is safe, without looking far ahead.

Let us say the flowerbed is [1, 0, 0, 0, 1] and n is 1. The middle plot at index 2 is empty. Its neighbors at index 1 and 3 are also empty. So you can plant one flower there. The answer is true.

Input: flowerbed = [1, 0, 0, 0, 1], n = 1
Output: true
Explanation: plant at index 2, since index 1 and index 3 are both empty

If n were 2 for the same bed, the answer would be false. There is just not enough room without two flowers touching.

Here is a picture of the example bed. The middle empty plot has empty neighbors, so it is plantable.

index 0: 1 flower

index 1: 0 empty

index 2: 0 plantable

index 3: 0 empty

index 4: 1 flower

🐒 Approach 1: Plant Then Re-Scan (Brute Force)

The idea in one line: plant one flower, scan the whole bed again, repeat.

The idea:

  • Find one safe empty plot.
  • Plant there and lower n by one.
  • Start the scan over from the beginning.

How it works:

  • Scan the array for a plot that is empty with empty neighbors.
  • Plant a flower there.
  • Restart the scan for the next flower.

Why it is weak:

  • Every flower you plant makes you scan the bed again.
  • In the worst case you scan many times.
  • That is O(nΒ²) time. A long bed gets slow.

Here is the brute-force code for that idea:

can_place_flowers_brute_force.py
def can_place_flowers(flowerbed, n):
bed = flowerbed[:]
def is_valid(index):
left_empty = index == 0 or bed[index - 1] == 0
right_empty = index == len(bed) - 1 or bed[index + 1] == 0
return bed[index] == 0 and left_empty and right_empty
planted = 0
for i in range(len(bed)):
if is_valid(i):
bed[i] = 1
planted += 1
if planted == n:
return True
return planted >= n
print(can_place_flowers([1, 0, 0, 0, 1], 1))

⚑ Approach 2: One Greedy Pass (Best)

The idea in one line: walk the bed once and plant at the first safe plot you reach.

The idea:

  • Walk the bed left to right one time.
  • Plant the moment a plot is safe.
  • Never go back.

How it works:

  • For each plot, check the plot itself is empty.
  • Check the left neighbor is empty or off the edge.
  • Check the right neighbor is empty or off the edge.
  • Off the edge means before index zero or after the last index, which counts as empty.
  • When all three hold, set the plot to 1 and lower n.
  • At the end, return true if n dropped to zero or below.

Why it is fast:

  • One forward pass touches each plot once. That is O(n).
  • It changes the bed in place, so no extra array. That is O(1).
  • Taking the earliest legal plot never costs a later one, so greedy is safe here.

This picture shows the greedy pass on [1, 0, 0, 0, 1] with n = 1. Follow the decision at each plot.

start: need = 1

index 0 = 1, skip

index 1 = 0, left is 1, not safe, skip

index 2 = 0, left 0 and right 0, plant, set 1, need = 0

index 3 = 0, left is now 1, not safe, skip

index 4 = 1, skip

need = 0, answer = true

Steps to Solve

  1. Walk the flowerbed from the first plot to the last.
  2. For the current plot, check that it is empty.
  3. Check that the left neighbor is empty or off the edge.
  4. Check that the right neighbor is empty or off the edge.
  5. If all three hold, set the plot to 1 and lower n by one.
  6. After the loop, return true if n is zero or less, otherwise false.

This Python version walks the bed once and treats off-the-edge plots as empty.

can_place_flowers.py
def can_place_flowers(bed, n):
size = len(bed)
for i in range(size):
empty = bed[i] == 0
left_free = i == 0 or bed[i - 1] == 0 # edge counts as empty
right_free = i == size - 1 or bed[i + 1] == 0 # edge counts as empty
if empty and left_free and right_free:
bed[i] = 1 # plant here
n -= 1
return n <= 0 # true if we planted enough
bed = [1, 0, 0, 0, 1]
n = 1
print(can_place_flowers(bed, n))

The output of the above code will be:

True

Let us walk through the Python version line by line. Code first, then the why.

def can_place_flowers(bed, n):
size = len(bed)
for i in range(size):
empty = bed[i] == 0
left_free = i == 0 or bed[i - 1] == 0
right_free = i == size - 1 or bed[i + 1] == 0
if empty and left_free and right_free:
bed[i] = 1
n -= 1
return n <= 0

for i in range(size): walks every plot once, left to right. We never go back. That single forward pass is what makes the greedy method fast.

empty = bed[i] == 0 checks that the current plot has no flower. We can only plant on an empty plot. So this must be true before we even look at the neighbors.

left_free = i == 0 or bed[i - 1] == 0 handles the left side and the left edge in one line. If i is zero, there is no left neighbor, so we treat it as empty. Otherwise the actual left plot must be empty. This i == 0 check is what stops the code from reading before the start of the array.

right_free = i == size - 1 or bed[i + 1] == 0 does the same for the right side. If i is the last index, there is no right neighbor, so it counts as empty. This guard stops the code from reading past the end of the array.

if empty and left_free and right_free: plants only when all three are safe. The plot is empty and both sides are clear. That is the exact rule of the problem.

bed[i] = 1 plants the flower. We change the bed itself, so the next step sees this new flower as the left neighbor. That is why we do not plant two in a row.

n -= 1 lowers how many we still need to plant.

return n <= 0 gives the final answer. If we planted at least as many as asked, n dropped to zero or below, so the answer is true.

⏱️ Time and Space Complexity

The brute force re-scans the bed for every flower, so its time grows as O(nΒ²). The greedy pass walks the bed just once and makes a safe local choice at each plot. So it is O(n) time. It changes the bed in place, so it uses O(1) extra space.

Approach Time Complexity Space Complexity
Brute force (re-scan per flower) O(nΒ²) O(1)
Single greedy pass O(n) O(1)

Tip

The edges are the trap. The first plot has no left neighbor and the last plot has no right neighbor. Treat those missing neighbors as empty. Say that out loud in the interview so they know you saw the edge case.

🧩 Key Takeaways

  • βœ… A 0 is an empty plot and a 1 is a plot that already has a flower.
  • βœ… You can plant on an empty plot only when both neighbors are empty.
  • βœ… Treat positions off the edge of the bed as empty.
  • βœ… The greedy choice of planting at the earliest safe plot is always correct.
  • βœ… One pass and in-place changes give O(n) time and O(1) extra space.

Check Your Knowledge

4 questions Show quiz Hide quiz

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    When can you plant a flower at a plot?

    Why: You can plant only when the plot itself is empty and both neighbors are empty or off the edge.

  2. 2

    How do you treat a neighbor that is off the edge of the bed?

    Why: There is nothing off the edge, so a missing neighbor counts as empty.

  3. 3

    Why does the greedy single pass work correctly?

    Why: Taking the earliest legal plot is always safe, so one forward pass is enough.

  4. 4

    What is the time and space complexity of the greedy solution?

    Why: One pass over the bed is O(n) time, and changing it in place uses O(1) extra space.

πŸš€ What’s Next?