Skip to content
Discussion options

You must be logged in to vote

We need to determine if all the 1's in a given binary array are at least k places away from each other. The solution involves iterating through the array, tracking the positions of the 1's, and checking the distance between consecutive 1's to ensure they meet the required separation.

Approach:

  1. Initialization: We initialize a variable to keep track of the last index where a 1 was found. Initially, this is set to -1 to indicate that no 1 has been encountered yet.
  2. Iteration: We iterate through each element of the array. For each element that is 1:
    • If this is not the first 1 encountered (i.e., lastOneIndex is not -1), we check the distance between the current 1 and the previous 1. The dista…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Nov 17, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 17, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants