-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1437.java
More file actions
33 lines (31 loc) · 912 Bytes
/
prblm1437.java
File metadata and controls
33 lines (31 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class prblm1437 {
public static void main(String[] args) {
int[] nums = {1,0,0,0,1,0,0,1};
int k = 2;
System.out.println(new prblm1437().kLengthApart(nums, k));
}
public boolean kLengthApart(int[] nums, int k) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
for (int j = i + 1; j <= i + k && j < nums.length; j++) {
if (nums[j] == 1) {
return false;
}
}
}
}
return true;
}
public boolean kLengthApart2(int[] nums, int k) {
int prev = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (i - prev - 1 < k) {
return false;
}
prev = i;
}
}
return true;
}
}