Header Ad

HackerEarth Aaryan Subsequences And Great XOR solution

In this HackerEarth Aaryan, Subsequences And Great XOR problem solution Aaryan went to school like any usual day, The teacher asked his crush the following question. Given an array of numbers, First, she had to compute the XOR of all the subsequences that can be formed. Suppose each subsequence had their following XOR value that came out after computing -> {P[0], P[1], P[2], and so on up to P[2^n-1] subsequences in an array of n numbers}

Now, the resultant answer is computed by taking bitwise inclusive OR of all P[i]'s

Since Aaryan wants to impress his crush, He wants to compute the answer for this problem but since he is not so good at it, he turned to you for help.


HackerEarth Aaryan, Subsequences And Great XOR problem solution


HackerEarth Aaryan, Subsequences And Great XOR problem solution.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <fstream>

using namespace std;

int n;
bool iff[32];

int main()
{
freopen("inp7.txt", "r", stdin);
freopen("out7.txt", "w", stdout);
int x;
scanf("%d", &n);
for ( int i = 0; i < n; i++ ) {
scanf("%d", &x);
for ( int j = 0; j < 30; j++ ) iff[j] = iff[j] | (x&(1<<j));
}
int ans = 0;
for ( int j = 0; j < 30; j++ ) {
if ( iff[j] ) ans += (1LL<<j);
}
printf("%d\n", ans);
return 0;
}


Second solution

#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)

typedef long long int LL;
int main() {
int n;
S(n);
assert(n >= 1 && n <= 1000000);
int ans = 0;
while(n--) {
int x;
S(x);
assert(x >= 1 && x <= 1000000000);

ans |= x;
}
P(ans);
return 0;
}


Post a Comment

0 Comments