Can Place Flowers
Problem Statement
A row of flowerbed plots is given. Each plot either already has a flower (1) or is empty (0). Flowers cannot be planted next to each other, so a new flower can only go on an empty plot whose neighbours are both empty. A missing neighbour (at the start or the end of the row) counts as empty.
Given the row and a number n, return true if you can plant n new flowers without breaking this rule, and false otherwise.
An array flowerbed of 0 and 1 values, and a number n. The row already obeys the rule: no two flowers are next to each other.
true if at least n new flowers fit, otherwise false.
Plant on index 2. Its neighbours, index 1 and index 3, are both empty.
Index 1 touches the flower at index 0, and index 3 touches the flower at index 4. Only index 2 is safe, so only one flower fits.
- • 1 ≤ flowerbed.length ≤ 2 · 10^4
- • Each value in flowerbed is 0 or 1.
- • No two flowers in flowerbed are next to each other.
- • 0 ≤ n ≤ flowerbed.length
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every set of plots and keep one that is valid and big enough
Scan left to right; plant wherever the plot and both neighbours are empty, then skip the next plot
Each plot can be planted or left empty, so there are 2^N sets of plots. For each set, check that every chosen plot is empty and that no chosen plot touches a flower or another chosen plot. If any valid set has at least n flowers, return true.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.