Zepruv › Coding problems

Check If All 1's Are at Least Length K Places Away

Easy · Array

Given an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.

Example 1

Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true

Each of the 1s are at least 2 places away from each other.

Example 2

Input: nums = [1,0,0,1,0,1], k = 2
Output: false

The second 1 and third 1 are only one apart from each other.

Constraints

Starter code (python)

class Solution(object):
    def kLengthApart(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        

Related problems

Previous: Count Good Nodes in Binary Tree · Next: Count Triplets That Can Form Two Arrays of Equal XOR · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.