Skip to content

Commit bd7a806

Browse files
authored
Merge pull request #24 from mrid88/feature/alterating-groups
Solution #3208 - Mridul/Edited - 09/03/2025
2 parents b112a43 + b10d157 commit bd7a806

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Explanation of the `numberOfAlternatingGroups` Method
2+
3+
The `numberOfAlternatingGroups` method in the `Solution` class takes an array of integers `colors` and an integer `k` as input, and returns the number of alternating groups of size `k` within the array.
4+
5+
### Code Breakdown
6+
7+
1. **Initialization**
8+
```java
9+
int window = 1;
10+
int res = 0;
11+
int n = colors.length;
12+
int i = 0;
13+
int j = 1;
14+
```
15+
- `window`: Keeps track of the current window size.
16+
- `res`: Stores the count of alternating groups of size `k`.
17+
- `n`: Stores the length of the `colors` array.
18+
- `i`, `j`: Pointers to traverse the array.
19+
20+
2. **While Loop to Traverse the Array**
21+
```java
22+
while(i < colors.length) {
23+
if(colors[(j-1) % n] != colors[j%n]) {
24+
window++;
25+
j++;
26+
}else {
27+
i = j;
28+
j = i + 1;
29+
window = 1;
30+
continue;
31+
}
32+
33+
if(window == k) {
34+
res++;
35+
if(colors[(j-1) % n] != colors[j%n]) {
36+
i++;
37+
window -= 1;
38+
}else {
39+
i = j;
40+
j = i + 1;
41+
window = 1;
42+
}
43+
}
44+
}
45+
```
46+
- The loop iterates through the array while `i` is less than the length of `colors`.
47+
- It checks if the current and the next colors are different. If they are different, the window size is incremented, and `j` is incremented.
48+
- If they are the same, it resets the window and continues the iteration.
49+
- When the window size reaches `k`, it increments the `res` count and adjusts the window accordingly.
50+
51+
3. **Return the Result**
52+
```java
53+
return res;
54+
```
55+
- Returns the total count of alternating groups of size `k`.
56+
57+
### Example
58+
59+
Given the `colors` array `[1, 2, 1, 2, 1]` and `k = 3`:
60+
- The method will return `2` since there are two alternating groups of size 3: `[1, 2, 1]` and `[2, 1, 2]`.
61+
62+
### Time Complexity
63+
- The time complexity of this method is O(n), where `n` is the length of the `colors` array, as it traverses the array a constant number of times.
64+
65+
### Space Complexity
66+
- The space complexity is O(n) as only a few variables are used, and no additional data structures are required.
67+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int numberOfAlternatingGroups(int[] colors, int k) {
3+
int window = 1;
4+
int res = 0;
5+
int n = colors.length;
6+
int i = 0;
7+
int j = 1;
8+
while(i < colors.length) {
9+
if(colors[(j-1) % n] != colors[j%n]) {
10+
window++;
11+
j++;
12+
}else {
13+
i = j;
14+
j = i + 1;
15+
window = 1;
16+
continue;
17+
}
18+
19+
if(window == k) {
20+
res++;
21+
if(colors[(j-1) % n] != colors[j%n]) {
22+
i++;
23+
window -= 1;
24+
}else {
25+
i = j;
26+
j = i + 1;
27+
window = 1;
28+
}
29+
}
30+
}
31+
32+
return res;
33+
}
34+
}

0 commit comments

Comments
 (0)