EasyArrayGreedyUpdated Sep 2026

605. Can Place Flowers

Asked at Atlassian, LinkedIn

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

2

Problem

You are given an array flowerbed that describes a row of plots. A 1 means a plot already has a flower, and a 0 means the plot is empty. You are also given an integer n.

Flowers may not be planted in two plots that touch each other. In other words, after you plant, no two 1s may sit side by side.

Return true if you can plant at least n new flowers without breaking that rule, and false otherwise. The input bed is guaranteed to already follow the rule, so you only need to worry about the flowers you add.

Where can a new flower go?

flowerbed1000101234Red plots are taken or blocked; plot 2 is the onlysafe spot
A plot is plantable only when it is empty and both of its neighbors are empty too.

Constraints

  • ●1 <= flowerbed.length <= 2 * 10^4
  • ●flowerbed[i] is 0 or 1
  • ●There are no two adjacent flowers in flowerbed
  • ●0 <= n <= flowerbed.length

Examples

Example 1
Inputflowerbed = [1,0,0,0,1], n = 1
Outputtrue
WhyPlot 2 is empty and both of its neighbors (plot 1 and plot 3) are empty, so we can plant one flower there. That gives 1 flower, which is enough.
Example 2
Inputflowerbed = [1,0,0,0,1], n = 2
Outputfalse
WhyAfter planting at plot 2 the bed is [1,0,1,0,1]. Every remaining empty plot now touches a flower, so only 1 new flower fits — not 2.

Asked At

2 companies
CompanyDifficulty
AtlassianEasyView all Atlassian questions →
LinkedInEasyView all LinkedIn questions →

How to Think About It

1

Baseline: try every combination of n empty cells and test adjacency - exponential and unusable. The greedy insight is that a locally safe cell is also globally safe because planting only ever blocks its two immediate neighbors.

2

Key insight: a cell can accept a flower iff it is empty and both immediate neighbors are empty or out of bounds. Planting there can never make a future decision worse, so greedy is equal to optimal.

3

The loop: scan left to right. When you find a plantable cell, plant, increment the count, and keep scanning - setting the cell to 1 makes the following cell's "previous empty" test fail automatically.

4

Why the mutation does the bookkeeping: flipping the current cell to 1 automatically blocks the next cell from being planted, so the array modification implements the adjacency rule with no extra state.

5

Edge cases: an empty bed returns True only when n is 0; a single cell [0] can take one flower; boundary cells need just one empty neighbor because the missing side is out of bounds.

Optimal Approach

Scan the flowerbed left to right. Plant at a cell when it is 0 and both neighbors are 0 or out of bounds. Set the cell to 1, increment the count, and continue; the mutation automatically blocks the following cell from also being planted. Return true when the count reaches n.

Walkthrough for flowerbed = [1, 0, 0, 0, 1], n = 1:

  1. i=0: value 1, skip.
  2. i=1: value 0, previous is 1, blocked.
  3. i=2: previous 0 and next 0, plant. count = 1, bed becomes [1, 0, 1, 0, 1].
  4. i=3: previous is 1, blocked. i=4: value 1, skip.
  5. count = 1 >= 1, return true.

Time: O(n) space: O(1).

Greedy left-to-right scan

flowerbed1000101234ii=1: empty, but the plot on the left holds a flower,so skipflowerbed1010101234ii=2: both neighbors empty, so plant here and set thecell to 1flowerbed1010101234ii=3: the plot on the left is now 1, so it is blockedautomaticallyplant if bed[i]==0 and left empty and right emptycount = 1 >= n = 1 -> true
Plant at the first safe plot, flip it to 1, and the flip itself blocks the next plot for free.
ComplexityTime O(n)Space O(1)We scan the bed once and only keep a counter, so the cost is one pass over the array.

What Trips People Up in Real Interviews

1

Handling the boundaries incorrectly. The first and last cells have a single neighbor - index out of bounds, or treating the missing neighbor as occupied, wrongly blocks a valid end-cell plant. Default the out-of-bounds side to empty.

2

Mutating the array and not accounting for the effect on the next cell. The mutation is exactly what makes greedy work - after planting at i, cell i+1 is automatically blocked; state it out loud so the interviewer knows it is deliberate.

3

Forgetting the trivial cases. Return True immediately when n = 0, and return n == 0 for an empty bed. Scanners that skip these can crash or report False for a zero-plant request.

4

Comparing with count == n instead of >= n. Because the bed mutates as you scan, an equality comparison is fragile - use >= n (or return True the moment count reaches n) to stay robust.

5

Reaching for sliding windows or DP. A single left-to-right greedy pass is the intended answer; extra machinery reads as pattern matching rather than understanding the local rule.

Solution Code

O(n) time
def canPlaceFlowers(flowerbed, n):
    count = 0
    for i in range(len(flowerbed)):
        if flowerbed[i] == 0:
            prev_empty = i == 0 or flowerbed[i - 1] == 0
            next_empty = i == len(flowerbed) - 1 or flowerbed[i + 1] == 0
            if prev_empty and next_empty:
                flowerbed[i] = 1
                count += 1
    return count >= n

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Can Place Flowers problem?

Given a flowerbed as a binary array where 1 means occupied and 0 means empty, decide whether n new flowers can be planted so no two flowers are adjacent. A one-pass greedy loop - the classic Atlassian entry point into the larger greedy family.

How do you solve Can Place Flowers?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Can Place Flowers?

Can Place Flowers is asked at Atlassian, LinkedIn. It is a easy difficulty problem.

What are common mistakes on Can Place Flowers?
  • Handling the boundaries incorrectly. The first and last cells have a single neighbor - index out of bounds, or treating the missing neighbor as occupied, wrongly blocks a valid end-cell plant. Default the out-of-bounds side to empty.
  • Mutating the array and not accounting for the effect on the next cell. The mutation is exactly what makes greedy work - after planting at i, cell i+1 is automatically blocked; state it out loud so the interviewer knows it is deliberate.
  • Forgetting the trivial cases. Return True immediately when n = 0, and return `n == 0` for an empty bed. Scanners that skip these can crash or report False for a zero-plant request.
  • Comparing with `count == n` instead of `>= n`. Because the bed mutates as you scan, an equality comparison is fragile - use `>= n` (or return True the moment count reaches n) to stay robust.
  • Reaching for sliding windows or DP. A single left-to-right greedy pass is the intended answer; extra machinery reads as pattern matching rather than understanding the local rule.