1437. Check If All 1’s Are at Least Length K Places Away LeetCode Solution

In this guide, you will get 1437. Check If All 1’s Are at Least Length K Places Away LeetCode Solution with the best time and space complexity. The solution to Check If All ‘s Are at Least Length K Places Away problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check If All ‘s Are at Least Length K Places Away solution in C++
  4. Check If All ‘s Are at Least Length K Places Away solution in Java
  5. Check If All ‘s Are at Least Length K Places Away solution in Python
  6. Additional Resources
1437. Check If All 1's Are at Least Length K Places Away LeetCode Solution image

Problem Statement of Check If All ‘s Are at Least Length K Places Away

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
Explanation: 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
Explanation: The second 1 and third 1 are only one apart from each other.

Constraints:

1 <= nums.length <= 105
0 <= k <= nums.length
nums[i] is 0 or 1

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1437. Check If All 1’s Are at Least Length K Places Away LeetCode Solution in C++

class Solution:
  def kLengthApart(self, nums: list[int], k: int) -> bool:
    if k == 0:
      return True

    n = len(nums)
    curr = 0
    next = 1

    while curr < n and next < n:
      if nums[next] == 1:
        if nums[curr] == 1 and next - curr <= k:
          return False
        curr = next
      next += 1

    return True
/* code provided by PROGIEZ */

1437. Check If All 1’s Are at Least Length K Places Away LeetCode Solution in Java

N/A
// code provided by PROGIEZ

1437. Check If All 1’s Are at Least Length K Places Away LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  2883. Drop Missing Data LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.