# How is this possible?

• ``````class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int i = 0;
int sz = flowerbed.size();
while(i < sz){
if(flowerbed[i] == 1)
i++;
else{
if(flowerbed[i-1] == 1)
i++;
else if(flowerbed[i+1] == 1)
i += 3;
else{
flowerbed[i] = 1;
i += 2;
n--;
}
}
}
return (n == 0);
}
};
``````

For the above code, I am getting true for
[0,0,1,0,0]
2,
but on submission, I am getting false.
Can someone explain?

• @RK21
Update :
Sorry, forgot to check for bounds, but still I am facing the same problem.

``````bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int i = 0;
int sz = flowerbed.size();
while(i < sz){
if(flowerbed[i] == 1)
i++;
else{
if(i > 0 && flowerbed[i-1] == 1)
i++;
else if(i < sz-1 && flowerbed[i+1] == 1)
i += 3;
else{
flowerbed[i] = 1;
i += 2;
n--;
}
}
}
return (n == 0);
}
``````

• Sahi baat hai

• Got it, need to check the value of n in while loop, to see if it doesn't go beyond zero.

• not beyond, below zero

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