``````class Solution {
public:
bool kLengthApart(vector<int>& nums, int k) {
int n = nums.size();

for(int i = 0;i < n;i++)
{
if(nums[i] == 1)
{
int cnt = k;
for(int j = i+1;j < n;j++)
{
if(nums[j] == 1)
{
if(cnt > 0)
return false;
break;
}
if(nums[j] == 0)
{
cnt--;
}
i = j;
}
}

}
return true;
}
};``````

``````Runtime: 44 ms, faster than 96.58% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.
Memory Usage: 57.5 MB, less than 72.40% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.``````

``````class Solution {
public:
bool kLengthApart(vector<int>& nums, int k) {
int n = nums.size();

for(int i = 0;i < n;i++)
{
if(nums[i] == 1)
{
int cnt = k;
while(i+1 < n)
{
if(nums[i+1] == 1)
{
if(cnt > 0) return false;
break;
}
i++;
cnt--;
}
}

}
return true;
}
};``````

``````Runtime: 44 ms, faster than 96.58% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.
Memory Usage: 57.6 MB, less than 35.66% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.``````

``````class Solution {
public:
bool kLengthApart(vector<int>& nums, int k) {
int prev = -k-1;
int n = nums.size();
for(int i = 0;i < n;i++)
{
if(1 == nums[i])
{
if(i - prev <= k)
return false;
else
prev = i;
}
}
return true;
}
};``````

``````Runtime: 60 ms, faster than 37.43% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.
Memory Usage: 57.4 MB, less than 93.03% of C++ online submissions for Check If All 1's Are at Least Length K Places Away.``````