Skip to main content

Can Place Flowers - Solution & Explanation

EasyArrayGreedy18 min readAsked at: Amazon, Microsoft, Apple +13
Practice this problem

Problem Statement

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.

 

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1
Output: true

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2
Output: false

 

Constraints:

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

Approach Overview

Problem Overview: You are given a binary array flowerbed where 1 means a flower is already planted and 0 means empty. You need to determine whether you can plant n new flowers without violating the rule that no two flowers can be adjacent.

Approach 1: Counting Possible Placements (O(n) time, O(1) space)

This method analyzes stretches of consecutive empty plots and calculates how many flowers can fit inside each segment. If you find a block of k zeros surrounded by planted flowers, the maximum placements inside that segment follow a predictable pattern: you can place (k-1)/2 flowers. Edge segments (start or end of the array) allow slightly more placements because only one side has a restriction. The algorithm scans the array once, counts zero segments, and accumulates the total possible placements. If the sum reaches or exceeds n, planting is feasible. The logic is purely arithmetic on segments, which keeps the time complexity O(n) and space complexity O(1). This technique works well when reasoning about contiguous gaps in an array.

Approach 2: Greedy Planting by Scanning (O(n) time, O(1) space)

The greedy approach directly simulates planting while scanning the array from left to right. For each index i, check three conditions: the current plot is empty, the left neighbor is empty (or doesn't exist), and the right neighbor is empty (or doesn't exist). If all conditions hold, plant a flower by setting flowerbed[i] = 1 and decrease n. This works because placing a flower as early as possible never blocks a better future placement. The algorithm only performs constant-time checks per position, so the total runtime is O(n) with O(1) extra space. This is a classic example of a greedy decision: a locally optimal placement leads to a globally optimal result.

During the scan you simply iterate once through the array, perform neighbor checks, and update the state when planting occurs. The modification is safe because once a flower is placed, the next index automatically becomes invalid for planting, which the neighbor checks naturally enforce.

Recommended for interviews: The greedy scanning approach is what interviewers typically expect. It shows you can reason about constraints directly and implement a linear pass with constant space. The counting approach demonstrates deeper pattern recognition with segments and boundaries, which can help when generalizing similar gap counting problems in array processing.

Approach 1: Greedy Planting by Scanning

This approach involves a single pass through the flowerbed array, considering each plot one by one. For each zero found, we check whether it's possible to plant a flower by ensuring that both neighboring plots (if they exist) are not planted. If it's possible, we plant the flower (i.e., change the zero to one) and reduce the count of n. The process stops early if n becomes zero. This greedy method ensures efficiency by trying to plant a flower at each opportunity.

This C solution iterates through the flowerbed, checking the conditions for planting flowers by ensuring adjacent plots are empty. It directly modifies the flowerbed array in the process, which simulates planting the flowers. If the required number of flowers (n) is planted, it returns early.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(m), where m is the length of the flowerbed array.
Space Complexity: O(1), as no extra space is used except for variables.

Try this approach in the editor →

Approach 2: Counting Possible Placements

This approach calculates the maximum number of flowers that can be planted by counting the stretches of zeros between planted flowers or array boundaries. For each encounter of a continuous stretch of zeros, determine how many flowers can be planted and accumulate this count until it reaches or exceeds n, or if it proves impossible.

This C solution leverages a counting mechanism, iterating through the flowerbed and adding cushions (zeros) at the boundaries to simplify calculations. The number of available spots for planting is incrementally calculated and checked against n.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(m), where m is the length of the flowerbed.
Space Complexity: O(1), only a constant amount of extra space is required.

Try this approach in the editor →

Approach 3: Greedy

We directly traverse the array flowerbed. For each position i, if flowerbed[i]=0 and its adjacent positions on the left and right are also 0, then we can plant a flower at this position. Otherwise, we cannot. Finally, we count the number of flowers that can be planted. If it is not less than n, we return true, otherwise we return false.

The time complexity is O(n), where n is the length of the array flowerbed. We only need to traverse the array once. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Planting by Scanning

Time Complexity: O(m), where m is the length of the flowerbed array.
Space Complexity: O(1), as no extra space is used except for variables.

Counting Possible Placements

Time Complexity: O(m), where m is the length of the flowerbed.
Space Complexity: O(1), only a constant amount of extra space is required.

Greedy—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Counting Possible PlacementsO(n)O(1)When analyzing zero segments or reasoning mathematically about placement capacity
Greedy Planting by ScanningO(n)O(1)General case and most common interview solution using greedy checks

Video Solution

Can Place Flowers - Leetcode 605 - Python • NeetCode • 66,332 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Can Place Flowers easy or hard?
Can Place Flowers is classified as an Easy problem on LeetCode. The main challenge is correctly handling boundary cases at the start and end of the array while applying a greedy placement rule.
Can Place Flowers Python/Java solution
Both Python and Java implementations follow the same greedy scan. Iterate through the array, check left and right neighbors, and plant when allowed. The logic is identical across Python, Java, C++, C#, and JavaScript with O(n) time and O(1) space complexity.
How to solve Can Place Flowers in O(n)?
Scan the flowerbed from left to right. For each index, check that the current position is 0 and both adjacent positions are also 0 (or out of bounds). If valid, plant a flower and decrement n. Continue until either n reaches zero or the array ends. This linear scan guarantees O(n) time and constant space.
What is the best approach for Can Place Flowers?
The greedy scanning approach is the most practical solution. Iterate through the array and plant a flower whenever the current plot and both neighbors are empty. Each placement updates the array and reduces the remaining flowers to plant. This runs in O(n) time with O(1) extra space and is the method most interviewers expect.
Is Can Place Flowers asked at Google/Amazon/Meta?
Variations of greedy array placement problems frequently appear in interviews at companies like Amazon, Google, and Meta. The problem tests basic greedy reasoning, edge case handling at boundaries, and careful array traversal.
What data structure is used in Can Place Flowers?
The problem primarily uses an array to represent the flowerbed. The solution relies on sequential iteration and neighbor checks rather than complex data structures, making it a straightforward greedy array problem.
What is the time complexity of Can Place Flowers?
The optimal solution runs in O(n) time because the flowerbed array is scanned once. Each position requires only constant-time neighbor checks. The space complexity is O(1) since the algorithm modifies the array in place and uses no additional data structures.

Ready to solve this problem?

Practice Can Place Flowers with our built-in code editor and test cases.

Practice on FleetCode