MAXAND18 - Editorial

Try using long long int

Hi

Link: CodeChef: Practical coding for everyone
This is my solution, can you please make me known about the problem due to which it is not passing the first subtask…? (It is successfully passing the second subtask)

I found a reply from @dean_student about this issue and used long long int and passed the subtask 1 too…, but I am still unable to identify when is the range for int exceeding here. Can someone please help me pointing that out…

Thanks in advance :smiley:

@dean_student please help

Even tried long long int now, still not getting it right. Can someone please help in finding mistake.

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
std::vectormp(33);

bool sort2 ( const vector& v1,const vector& v2 ) {
if(v1[1]==v2[1]){
return v1[0] < v2[0];
} else return v1[1]>v2[1];
}

void bitcount( ll n){
ll i=0;
while (n){
if(n%2==1)mp[i]++;
i++; n/=2;
}
}

int main(){
int t; cin>>t;
while(t–){
ll n, k; cin>>n>>k;
ll a[n];
for(int i=0; i<n; i++){
cin>>a[i];
bitcount(a[i]);
}

    vector<vector<ll> >v(33, vector<ll>(2));
    for(int i=0; i<mp.size(); i++){
        v[i][0]=i;
        v[i][1]=mp[i]*pow(2,i);
    }
    sort(v.begin(), v.end(), sort2);

    vector<ll>ans;
    for(int i=0; i<k; i++){
        ans.push_back(v[i][0]);
    }
    ll as=0;
    for(int i=0; i<ans.size(); i++){
        as+=pow(2,ans[i]);
    }
    cout<<as<<endl;
}

}

@dean_student please help me :neutral_face:

Hello ,can you please tell me what is bcount here ?

bcount is a counter of how many numbers have the bth bit turned on.

I have used long long int aldready
#define int long long int

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct piyush
{
long long int power;
long long int pr;
long long int fre;
};
int comparator(const void* p, const void* q)
{
return (((struct piyush*)p)->pr - ((struct piyush*)q)->pr);
}
void decToBinary(long long int n,struct piyush binaryNum[])
{
// array to store binary number

// counter for binary array 
long long int i = 0; 
while (n > 0) { 

    // storing remainder in binary array 
    binaryNum[i].fre = binaryNum[i].fre+(n % 2); 
    n = n / 2; 
    i++; 
} 

}
int main()
{
long long int n,i,j,k,t;
scanf("%lld",&t);
while(t>0)
{
scanf("%lld%lld",&n,&k);
long long int a[n];
long long int maxpower=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
long long int o=log2(a[i])+1;
if(o>maxpower)
maxpower=o;
}
struct piyush ans[maxpower];
long long int val=1;
for(i=0;i<maxpower;i++)
{
if(i==0)
ans[i].power=1;
else
{val=val2;
ans[i].power=val;
}
ans[i].pr=0;
ans[i].fre=0;
}
for(i=0;i<n;i++)
{
decToBinary(a[i],ans);
}
for(i=0;i<maxpower;i++)
{
ans[i].pr=(ans[i].power)
(ans[i].fre);
}
qsort(ans,maxpower, sizeof(struct piyush), comparator);
long long int sum=0;
for(i=maxpower-1;i>=0;i–)
{
k–;
sum=sum+(ans[i].power);
if(k<=0)
break;
}
printf("%lld\n",sum);
t–;
}
}
why is this wrong please help me !!!

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct piyush
{
long long int power;
long long int pr;
long long int fre;
};
int comparator(const void* p, const void* q)
{
return (((struct piyush*)p)->pr - ((struct piyush*)q)->pr);
}
void decToBinary(long long int n,struct piyush binaryNum[])
{
// array to store binary number

// counter for binary array 
long long int i = 0; 
while (n > 0) { 

    // storing remainder in binary array 
    binaryNum[i].fre = binaryNum[i].fre+(n % 2); 
    n = n / 2; 
    i++; 
} 

}
int main()
{
long long int n,i,j,k,t;
scanf("%lld",&t);
while(t>0)
{
scanf("%lld%lld",&n,&k);
long long int a[n];
long long int maxpower=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
long long int o=log2(a[i])+1;
if(o>maxpower)
maxpower=o;
}
struct piyush ans[maxpower];
long long int val=1;
for(i=0;i<maxpower;i++)
{
if(i==0)
ans[i].power=1;
else
{val=val2;
ans[i].power=val;
}
ans[i].pr=0;
ans[i].fre=0;
}
for(i=0;i<n;i++)
{
decToBinary(a[i],ans);
}
for(i=0;i<maxpower;i++)
{
ans[i].pr=(ans[i].power)
(ans[i].fre);
}
qsort(ans,maxpower, sizeof(struct piyush), comparator);
long long int sum=0;
for(i=maxpower-1;i>=0;i–)
{
k–;
sum=sum+(ans[i].power);
if(k<=0)
break;
}
printf("%lld\n",sum);
t–;
}
}can you help me bro please!!! I done the same thing please help meee!!!
thanks in advance :smiley:

why the code is wrong??

  • if i use int bit[32] to store the bit count then i get AC
  • if i use map<int,int> then i get partially correct
  • Acc to me i shd not get any difference even in time complexity bcoz map here takes atmost log(32) but why such difference
#include <bits/stdc++.h>
using namespace std;
#define int long long int

bool f(pair<int, int> p1, pair<int, int> p2) {
    if (p1.second == p2.second) {
        return p1.first < p2.first;
    }
    return p1.second > p2.second;
}

void solve() {
    int i, j, n, k;
    cin >> n >> k;
    int a[n];
    map<int, int> hash;
    // int bits[32] = {};
    for (i = 0; i < n; ++i) {
        cin >> a[i];
        int temp = a[i];
        j = 0;
        while (temp) {
            if (temp & 1) {
                ++hash[j];
                // ++bits[j];
            }
            temp >>= 1;
            ++j;
        }
    }
    vector<pair<int, int>> v;
    for (auto pr : hash) {
        // for (i = 0; i < 32; ++i) {
        // v.pb({i, (bits[i] * (1 << i))});
        v.push_back({pr.first, (pr.second * (1 << pr.first))});
    }
    sort(v.begin(), v.end(), f);
    // print(bits);
    int res = 0;
    for (i = 0; i < k; ++i) {
        res += (1 << (v[i].first));
    }
    cout << res << endl;
}

int32_t main() {
    int _t = 1;
    cin >> _t;
    while (_t--)
        solve();
}

Can someone help me find where i went wrong in this solution (It is implemented in python 3) ??
34812408

Testcases were a bit weak you could say. Problem in your code is overflow occurs. you wont be able to store x*(2^y) in an int where 1<=x<=n and 0<=y<=29. It has nothing to do with value of K or sub-tasks.

1 Like

CodeChef: Practical coding for everyone Try this, easy and simple code.

CodeChef: Practical coding for everyone Try this, simple and easy code.

CodeChef: Practical coding for everyone @mandalorian @codetoplay

but why is mine wrong

Can’t find where is this code going wrong, it got WA in first subtask and RE in Subtask 2. Thanks in advanced.

#include <bits/stdc++.h>

#include <assert.h>

//shorts

#define ll long long int

#define vll vector<long long int>

#define vvll vector<vector<long long int>>

#define vpll vector<pair<long long int, long long int>>

#define sll set<long long int>

#define mp make_pair

#define pb push_back

#define endl "\n"

#define here std::cout << "here\n";

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);

#define MOD 1000000007

//program specific shorts (if any)

#define MAX 31

using namespace std;

//attributed to gfg

void decToBinary(ll n, ll bitKeep[MAX]) { 

    ll i = 0; 

    while (n > 0) { 

        if(n & 1)

            bitKeep[i]++;

        n /= 2; 

        i++; 

    }

} 

int main() {

    fastIO;

    ll t; cin >> t;

    for(ll q = 0; q < t; q++) {

        ll bitKeep[MAX] = {0};

        ll n, k; cin >> n >> k;

        ll a[n];

        for(ll i = 0; i < n; i++) {

            cin >> a[i];

            decToBinary(a[i], bitKeep);

        }

        vpll vals;

        for(ll i = 0; i < MAX; i++) {

            if(bitKeep[i])

                vals.pb(mp(bitKeep[i] * (1<<i), i));

        }

        sort(vals.rbegin(), vals.rend());

        ll ansArr[MAX] = {0};

        for(ll i = 0, x = 0; i < MAX && x < k; i++, x++)

            ansArr[vals[i].second] = 1;

        ll ans = 0;

        for(ll i = 0; i < MAX; i++)

            if(ansArr[i])

                ans += (1<<i);

        cout << ans << endl;

    }

    return 0;

}

Rating update is taking quite a time now