605. Can Place Flowers - Detailed Explanation

Problem statement

Description:
You are given a flowerbed represented as an integer array flowerbed containing 0's and 1's. A 0 means an empty plot, and a 1 means a plot that already holds a flower. Flowers cannot be planted in adjacent plots. Given an integer n, determine whether n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.

Example 1:

  • Input:
    • flowerbed = [1, 0, 0, 0, 1]
    • n = 1
  • Output: true
  • Explanation:
    You can plant a flower in the middle plot (index 2) to get [1, 0, 1, 0, 1].

Example 2:

  • Input:
    • flowerbed = [1, 0, 0, 0, 1]
    • n = 2
  • Output: false
  • Explanation:
    There is only one spot available to plant a new flower without violating the adjacent rule.

Example 3:

  • Input:
    • flowerbed = [0, 0, 1, 0, 0]
    • n = 2
  • Output: true
  • Explanation:
    One valid planting places flowers at indices 0 and 4, which gives [1, 0, 1, 0, 1]. Index 3 would be illegal, because it sits next to the existing flower at index 2. Checking both neighbors before planting is what prevents that mistake.

Constraints:

  • 1 ≤ flowerbed.length ≤ 2 * 10⁴
  • flowerbed[i] is 0 or 1
  • There are no two adjacent flowers in flowerbed initially
  • 0 ≤ n ≤ flowerbed.length

The technique here is a greedy single pass. You scan the flowerbed once, plant whenever a plot and both of its neighbors are empty, and never revisit a decision. Here a reading index i moves forward while the neighbor positions i-1 and i+1 decide whether the plot at i is usable. A separate counter records how many flowers you have placed. That arrangement is what lets a single sweep answer the question, and it recurs across the array patterns that interviewers reuse. Design Gurus teaches this exact problem in the Can Place Flowers lesson.

Hints

  • Hint 1:
    Iterate through the array. At each empty plot, check whether its adjacent plots, or the boundaries, are empty.

  • Hint 2:
    Treat the boundaries carefully. If the plot sits at the start or the end of the array, you only need to check one neighbor.

  • Hint 3:
    When you plant a flower, mark that plot as planted by setting it to 1. Then skip the next plot, because it can no longer hold another flower.

Approaches

Approach 1: greedy iteration

  • Idea:
    Traverse the flowerbed. For each plot, check whether it is empty and whether its neighbors, where they exist, are also empty. If both hold, plant a flower by setting that plot to 1 and decrement n. Continue until the scan ends or n reaches 0.
  • Details:
    • For index i, check if flowerbed[i] == 0.
    • For the left neighbor: if i == 0 or flowerbed[i-1] == 0.
    • For the right neighbor: if i == flowerbed.length - 1 or flowerbed[i+1] == 0.
    • If both conditions are true, plant a flower (flowerbed[i] = 1) and skip the next index.
  • Benefits:
    This method is direct, and it runs in O(n) time with O(1) extra space. The greedy rule is safe because planting at the earliest legal plot never removes a later option.

Approach 2: simulation with in-place updates

  • Idea:
    Use a similar method to the greedy approach, but simulate the planting process more explicitly. Update the array in place and count the planted flowers.
  • Benefits:
    This method matches the greedy approach in behavior and also uses constant extra space.

Code implementations

Python code

Python3
Python3

. . . .

Java code

Java
Java

. . . .

Complexity analysis

  • Time complexity:
    The algorithm scans the flowerbed once, so the time complexity is O(n), where n is the length of the flowerbed.

  • Space complexity:
    The solution uses a constant amount of extra space, O(1).

Step-by-step walkthrough

  1. Initialize a counter:

    • Start with count = 0 to count the number of flowers planted.
  2. Iterate through the flowerbed:

    • For each index i, check whether flowerbed[i] is 0, meaning an empty plot.
  3. Check neighbors:

    • For the left neighbor, check whether i is 0 or flowerbed[i-1] is 0.
    • For the right neighbor, check whether i is the last index or flowerbed[i+1] is 0.
  4. Plant a flower:

    • If both neighbor checks pass, set flowerbed[i] to 1, which simulates planting a flower.
    • Increment the count of planted flowers.
    • If count reaches n, return true.
  5. Finish the loop:

    • After the scan ends, return true if count is at least n. Otherwise return false.

Visual example

Consider the flowerbed [1, 0, 0, 0, 1] with n = 1:

  • Index 0:
    Value is 1, so skip.

  • Index 1:
    Value is 0.

    • Left neighbor (index 0) is 1 → cannot plant here.
  • Index 2:
    Value is 0.

    • Left neighbor (index 1) is 0 and right neighbor (index 3) is 0 → plant a flower here.
    • Set flowerbed[2] = 1 and increment count to 1.
  • Since count equals n, return true.

The updated flowerbed becomes [1, 0, 1, 0, 1].

Common mistakes

  • Ignoring boundaries:
    Failing to check whether the current index sits at the start or the end of the array can raise an index out-of-bounds error.
  • Not updating the array:
    Forgetting to mark the plot as planted, by setting it to 1, leads to an incorrect count.
  • Overplanting:
    Skipping the wrong number of plots after planting can place two flowers side by side, which violates the rule.

Edge cases

  • No available spots:
    When the flowerbed is full or nearly full, the algorithm should return false.
  • n equals 0:
    If no new flower needs to be planted, the function should return true immediately.
  • A single-plot flowerbed:
    Handle the case where the flowerbed contains only one plot, since that plot has no neighbor on either side.
  • Modified constraints:
    Variants might set a different minimum gap between flowers, or introduce several types of flowers with their own rules.
  • Other greedy problems:
    The same greedy reasoning applies to interval scheduling, seating arrangements, and resource allocation questions.
  • Gas Station
  • Jump Game
  • Container With Most Water
  • Merge Intervals

Two more single-scan array questions pair well with this one. The answer to Product of Array Except Self comes out of a prefix pass and a suffix pass. In Reverse Words in a String, two indices mark the bounds of each word as you walk the string. Both, along with the rest of the array set, are taught in Grokking the Coding Interview.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is software testing in QA?
What design methods do you use?
What is asked in Google interview?
What to Wear to a Teams Interview (Video Dress Code)
Business casual in a solid mid-tone color is right for almost every Microsoft Teams interview. Skip pure white, small patterns and logos.
What Is the Full Form of Amazon?
Amazon has no full form because it is not an acronym, and its full legal name is Amazon.com, Inc.
Who is eligible for Adobe placement?
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.