In this Leetcode Game of Deletion problem solution, Two players are playing the game of deletion. Player1 plays with array A and Player2 plays with array B. The length of both arrays is N.
The game is like this - both of them want to delete their respective arrays. A player can delete the array in multiple steps.
During one step, he chooses some numbers from the array and takes the bitwise OR of these numbers, and removes these numbers from the array.
This bitwise OR is the cost incurred in deleting the selected numbers. This way he deletes his whole array. The reward of deleting the entire array will be the sum of all numbers in the array minus the sum of the cost of all the steps.
The player with the maximum reward will win.

You have to print the winner along with the margin of reward he wins with. Let's say Player1 scored reward1 and Player2 scored reward2 and Player1 wins, then the margin will be reward1 - reward2.


HackerEarth Game of Deletion problem solution


HackerEarth Game of Deletion problem solution.

#include<bits/stdc++.h>
using namespace std;
#define test() int t;scanf("%d",&t);for(int tno=1;tno<=t;tno++)
#define mp make_pair
#define pb push_back
#define wl(n) while(n--)
#define fi first
#define se second
#define all(c) c.begin(),c.end()
typedef long long ll;
typedef unsigned long long llu;
typedef vector<int> vi; 
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define sz(a) int((a).size())
#define ini(a,v) memset(a,v,sizeof(a))
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scs(s) scanf("%s",s);
#define gcd __gcd
#define debug() printf("here\n") 
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a,b) cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << endl
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++) 
#define MOD 1000000007
#define inf ((1<<29)-1)
#define linf ((1LL<<60)-1)
const double eps = 1e-9;

const int MAX = 200009;

ll a[MAX]={0};
ll b[MAX];
int main()
{
    int i,j,k;
    int n;
    sc(n);
    ll sum1 = 0,sum2 = 0;
    ll c1 = 0,c2 = 0;
    for(i=0;i<n;i++){
        scl(a[i]);
        sum1+=a[i];
        c1 = c1 | a[i];
    }
    for(i=0;i<n;i++){
        scl(b[i]);
        sum2+=b[i];
        c2 = c2 | b[i];
    }
    sum1 -= c1;
    sum2 -= c2;
    if(sum1>sum2){
        cout<<1<<" "<<sum1-sum2<<"\n";
    }else if(sum1<sum2){
        cout<<2<<" "<<sum2-sum1<<"\n";
    }else{
        printf("-1\n");
    }
    return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;

#include<limits>
#define ll long long

typedef pair<int, int > pii;
#define pb push_back
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
#define rep(p,q,r) for(int p=q;p<r;p++)
#define repr(p,q,r) for(int p=q;p>=r;p--)
#define TEST int test; cin >> test;while(test--)
#define si(x) scanf("%d",&x)
#define author real_flash
#define si2(x,y) scanf("%d %d",&x,&y)
#define si3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define prl(x) printf("%lld\n",x)
#define ff first
#define ss second
#define BE(a) a.begin(), a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define INF 111111111111111LL
#define mo 1000000007
int MAX=numeric_limits<int>::max();
const int N=1e6+5;

int n;
ll a[N],b[N],or1,or2,s1,s2;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("smin.txt", "r", stdin);
    freopen("smout.txt", "w", stdout);      
#endif
    si(n);
    assert(n>=1&&n<=100000);
    rep(i,0,n)
    {
        sl(a[i]);
        assert(a[i]>=1&&a[i]<=100000);
        or1=or1|a[i];
        s1+=a[i];
    }
    rep(i,0,n)
    {
        sl(b[i]);
        or2=or2|b[i];
        s2+=b[i];   
    }
    s1-=or1;
    s2-=or2;
    if(s1>s2)
        cout<<"1 "<<s1-s2<<"\n";
    else if(s2>s1)
        cout<<"2 "<<s2-s1<<"\n";
    else cout<<"-1\n";
}