# Javascript Solution O(N) Very easy to Understand with explanation

• Only one variable is needed so it's O(1) space complexity.
It will iterate through each item in the array so O(N) speed where N is the length of the array.

It will check if it's possible to plant at that iterator by getting the sum of its adjacent values and if it's 0, add 1 to possible places to plant and assign 1 so it's counted for the next values.

``````var canPlaceFlowers = function(flowerbed, n) {
var maxPlant = 0;

if (flowerbed.length > 1){
for (var i = 0; i < flowerbed.length; i++){
if (i  === 0){
if (flowerbed[i] + flowerbed[i+1] === 0){
maxPlant++;
flowerbed[i] = 1;
}
}
else if (i === flowerbed.length-1){
if (flowerbed[i-1] + flowerbed[i] === 0){
maxPlant++;
}
}
else if (flowerbed[i] + flowerbed[i-1] + flowerbed[i+1] === 0){
maxPlant++;
flowerbed[i] = 1;
}
}
return maxPlant >= n ? true: false;
}
else {
if (flowerbed[0] === 0) return true;
else{
return n === 0 ? true: false;
}
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.