Maximum AND

Can anyone tell, what’s wrong with my code.
I got only 50 points.
Maximum AND - CodeChef: Practical coding for everyone

1 Like

try:
1
4 3
1 1 1 1
Ans should be: 7

Yes, i got 7

Try
1
4 1
0 0 0 0

Ai is constrained to be greater than 1. Invalid test case

I got 1 but it is invalid test case

define i as long long and update (1<<i) to (1LL<<i)
try this

2 necessary changes changes:

  1. Change the data type to long long.
  2. for(int i=0;i<32;i++) this gives you a wrong output , change 32 to 30
    as 1<<31 gives some random output, or as @sumit_saraff said.

your modifed code - CodeChef: Practical coding for everyone

@sajan123456 see this

#include "bits/stdc++.h"
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define lld long long int

bool sortinrev(const pair<lld,lld> &a,
               const pair<lld,lld> &b)
{
       return (a.first > b.first);
}

int main() {
	// your code goes here
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        lld bits[36];
        int n,k;
        cin>>n>>k;
        long arr[100001];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
            for(int j=0;j<=35;j++)
            {
                lld temp=arr[i];
                bits[j]+=(1 & temp>>j);
            }
        }
        vector <pair <lld,lld> > v;
        lld power=1;
        for(int i=0;i<=35;i++)
        {
            v.push_back({bits[i]*power,i});
            power*=2;
        }
        sort(v.begin(),v.end(),sortinrev);
        int mark[36]={0};

        //for(int i=0;i<34;i++)
          //  cout<<v[i].first<<" "<<v[i].second<<"\n";

        int ptr=0;

        set <int> :: iterator it1;
        if(k>0)
        {
            for(int i=0;i<v.size();i++)
            {
                if(k<=0)
                break;
                 set <int> s;
                lld val=v[i].first;
                lld in=v[i].second;
                s.insert(in);
                while(i+1<v.size()&&v[i+1].first==val)
                {
                    i++;
                    s.insert(v[i].second);
                }
                for(it1=s.begin();it1!=s.end();it1++)
                {
                    if(k<=0)
                        break;
                    mark[*it1]=1;
                    k--;
                }
                s.clear();
            }
        }
        lld ans=0;
        power=1;
        for(int i=0;i<=35;i++)
        {
            if(mark[i]==1)
            {
                ans+=power;
            }
            power*=2;
        }
        cout<<ans<<"\n";
    }
}

Two tc are passing. Can you tell my mistake

in MAXAND sum, my subtask1 is failing and subtask2 is correct. What can be possible error for K<=2 case. Any hints?
EDIT: Passed. Error was that I did not use 1LL<<i.

1 Like

I’m having similar trouble here.
I’m never doing 1 << 31, as that’s way beyond the given limits. Can you see what’s wrong?

[EDIT]: Nevermind, I had done 1 << 31 in sorting. Now is passed.

[EDIT]: Sorry if the REP macro annoyed you. I’m not very sure how I got sucked into using such a thing.

1 Like

Thanks bro @rishabh7896 @sumit_saraff

1 Like