MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / numberOfAlternatingGroups

Method numberOfAlternatingGroups

AlternatingGroupsII.java:2–18  ·  view source on GitHub ↗
(int[] colors, int k)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int numberOfAlternatingGroups(int[] colors, int k) {
3 int res=0;
4 int left=0;
5 int n = colors.length;
6 // N+K
7 for(int right=1;right < (n + k -1); right++){ // exp..
8 // skip entire subarray
9 if(colors[right%n] == colors[(right-1)%n]){
10 left = right;
11 }
12 if(right - left + 1 == k){
13 res++;
14 left++; // move to next subarray or shrinking phase
15 }
16 }
17 return res;
18 }
19}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected