Repository navigation
Expand file tree
/
Copy pathAlternatingtile.java
More file actions
72 lines (55 loc) · 1.81 KB
/
Copy pathAlternatingtile.java
File metadata and controls
72 lines (55 loc) · 1.81 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Alternatingtile {
public static void main(String[] args) {
int[] colors = {0,1,0,1,0};
int k = 3 ;
// int n = colors.length ;
// int[] cir = new int[n + k];
// for(int i = 0 ; i < colors.length ; i++){
// cir[i] = colors[i];
// }
// for(int i = 0 ; i < k ; i++){
// cir[n+i] = colors[i];
// }
// for(int i = 0 ; i < cir.length ; i++){
// System.out.println(cir[i]);
// }
int m = numberOfAlternatingGroups(colors, k);
System.out.println(m);
}
public static int numberOfAlternatingGroups(int[] colors, int k) {
int n = colors.length;
int[] cir = new int[n + k - 1];
if(n==1){
return 0 ;
}
if(k == 1){
return n ;
}
for (int i = 0; i < colors.length; i++) {
cir[i] = colors[i];
}
for (int i = 0; i < k - 1; i++) {
cir[n + i] = colors[i];
}
// for(int i = 0 ; i < cir.length ; i++){
// System.out.println(cir[i]);
// }
int count = 0;
for (int i = 0; i < n ; i++) {
int flag = 0;
for (int j = i + 1 ; j < i + k - 1 ; j++) {
// int left = i;
// int right = i + k;
// int mid = (left + right) / 2;
if (cir[j] == cir[j + 1] || cir[j] == cir[j - 1]) {
flag = 1;
break ;
}
}
if (flag == 0) {
count++;
}
}
return count ;
}
}