Codeforces|Leetcode|Codechef free solutions
Open in Telegram
Free codeforces, Codechef, Leetcode solutions are available 😍😍😍😍😍😍 Helped More than 200+ students to crack coding round in 2022 and helped placed them in Good companies. 🥳🥳🥳🤩🤩🤩 Dm @Cpsoln if you want help in coding round.
Show more4 317
Subscribers
No data24 hours
-137 days
-5230 days
Posts Archive
int numberOfAlternatingGroups(vector& colors, int k) {
int n = colors.size();
if (n < k) return 0;
int cntrr = 0;
bool isAlternating = true;
for (int i = 0; i < k - 1; ++i) {
if (colors[i] == colors[i + 1]) {
isAlternating = false;
break;
}
}
if (isAlternating) cntrr++;
for (int i = 1; i < n; ++i) {
int outgoing = colors[(i - 1) % n];
int incoming = colors[(i + k - 1) % n];
int next = colors[(i + k) % n];
int prev = colors[(i - 2 + n) % n];
if (outgoing == prev) isAlternating = false;
if (incoming == next) isAlternating = false;
if (outgoing != prev && incoming != next) isAlternating = true;
if (isAlternating) cntrr++;
}
return cntrr;
}
Subscribe and react kro guys
Then only I will upload b and c codes here
long long countSubarrays(vector& nums, int k) {
long long count = 0;
int n = nums.size();
for (int start = 0; start < n; ++start) {
int current_and = nums[start];
if (current_and == k) {
count++;
}
for (int end = start + 1; end < n; ++end) {
current_and &= nums[end];
if (current_and == k) {
count++;
} else if (current_and < k) {
break; // If current_and < k, no need to extend further
}
}
}
return count;
}
int countAlternatingGroups(vector& colors) {
int n = colors.size();
int count = 0;
for (int i = 0; i < n; ++i) {
if (colors[i] != colors[(i+1) % n] && colors[i] == colors[(i+2) % n]) {
count++;
}
}
return count;
}
