# MAKEZERO-Editorial

Setter: Yash Gandhi
Tester: Manan Grover, Lavish Gupta
Editorialist: Devendra Singh

1320

None

# PROBLEM:

You are given an array A of length N.

You can perform the following operation on the array any number of times:

• Choose any subsequence S of the array A and a positive integer X such that X is a power of 2 and subtract X from all the elements of the subsequence S.

Find the minimum number of operations required to make all the elements of the array equal to 0.

# EXPLANATION:

Any two operations have different values of X in the optimal strategy

Let us suppose the subsequences chosen in two operations having same value of X be S_1 and S_2. Select all the elements which are present in both the subsequences and put them in new\:S_2 and and apply the operation on them taking 2\cdot X and the elements which are present only in either S_1 or only in S_2 into new\: S_1 and apply the operation by taking X as the integer. Keep doing this until there are no two operations with same value of X.

This means for each bit i which is set to 1 in the binary representation of any element K of the array A , 2^i will be subtracted, from all the elements having i^{th} bit set to 1, exactly once.
Therefore the answer is the number of different bits which are set to 1 in any element of the array.
Let OrValue be the bitwise\:OR of all the elements of the array.

# TIME COMPLEXITY:

O(N) for each test case.

# SOLUTION:

Setter's solution
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}

if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}

return x;
} else {
assert(false);
}
}
}
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}
int sumN=0;
int MaxAi=1000000000;
void solve()
{
int A[n+1]={0};
for(int i=1;i<=n;i++)
{
if(i==n)
else
}
ll orval=0;
for(int i=1;i<=n;i++)
orval|=A[i];
cout<<(__builtin_popcount(orval))<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
//cin>>T;
while(T--)
solve();
assert(getchar()==-1);
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

Editorialist's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
const int N = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
void sol(void)
{
int n, ans = 0;
cin >> n;
vll v(n);
int bit[30] = {0};
for (int i = 0; i < n; i++)
{
cin >> v[i];
for (int j = 0; j < 30; j++)
if ((1 << j) & v[i])
bit[j]++;
}
for (int j = 0; j < 30; j++)
ans += (bit[j] > 0);
cout<<ans<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int test = 1;
cin >> test;
while (test--)
sol();
}



for (int j = 0; j < 30; j++)
if ((1 << j) & v[i])
bit[j]++;

in this part of the code.,why do we only shift bits till the 29th position is actually an integer in a 32-bit number?