You have a flowerbed represented as a list of 0s and 1s, where 0 means empty and 1 means already planted. Flowers cannot be planted in adjacent plots. Given the flowerbed and an integer n, return true if n new flowers can be planted without violating the no-adjacent rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Explanation: One flower can be planted at index 2, which is not adjacent to either existing flower.
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Explanation: Only one empty non-adjacent spot exists, so two flowers cannot be placed.
Example 3:
Input: flowerbed = [0,0,0], n = 2
Output: true
Explanation: Plant at index 0 and index 2 for a total of two flowers.
Example 4:
Input: flowerbed = [1,0,1], n = 1
Output: false
Explanation: The only empty plot at index 1 is flanked by flowers on both sides.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.