Code : Birthday Gift
#include <bits/stdc++.h>
using namespace std;
bool is_bit_set(int x, int i) { return ((1 << i) & x) != 0; }
bool is_rhs_submask(int mask, int sub) { return (mask | sub) == mask; }
int solve(vector<int> &a, int x) {
x++;
int n = a.size();
int ans = -1;
for (int bit = 0; bit <= 30; bit++) {
if (!is_bit_set(x, bit)) {
continue;
}
int goal = (x >> bit) - 1;
int parts = 0, pref = 0, have = 0;
for (int i = 0; i < n; i++) {
pref ^= (a[i] >> bit);
if (is_rhs_submask(goal, pref)) {
have |= pref;
pref = 0;
parts++;
}
}
// Code will work even without have == goal check. Why?
if (is_rhs_submask(goal, pref) && have == goal) {
ans = max(ans, parts);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = solve(a, x);
cout << ans << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
bool is_bit_set(int x, int i) { return ((1 << i) & x) != 0; }
bool is_rhs_submask(int mask, int sub) { return (mask | sub) == mask; }
int solve(vector<int> &a, int x) {
x++;
int n = a.size();
int ans = -1;
for (int bit = 0; bit <= 30; bit++) {
if (!is_bit_set(x, bit)) {
continue;
}
int goal = (x >> bit) - 1;
int parts = 0, pref = 0;
for (int i = 0; i < n; i++) {
pref ^= (a[i] >> bit);
if (is_rhs_submask(goal, pref)) {
parts++;
pref = 0;
}
}
if (is_rhs_submask(goal, pref)) {
ans = max(ans, parts);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = solve(a, x);
cout << ans << '\n';
}
return 0;
}