Skip to content
Related Articles
Open in App
Not now

Related Articles

Google Online Challenge 2020

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 22 Jul, 2022
Improve Article
Save Article

The Google online challenge 2020 for summer internships 2021 was held on August 16. It was a 60-minute online test having 2 questions to code.

First Question: Size of the smallest subset with maximum Bitwise OR

Second Question: Given a list which initially contains 0, the following queries can be performed:

  • 0 X: add X to the list
  • 1 X: replace each element “A” of the list by A^X, where ^ is the xor operator.

Return a list with the result elements in increasing order.

Example:

5 (no of queries)
0 4
0 2
1 4
0 5
1 8

Answer:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int q;
        cin >> q;
        priority_queue<pair<int, int> > pq;
        vector<int> v;
        pq.push({
            0,
            0,
        });
        v.push_back(0);
        while (q--) {
            int a, x;
            cin >> a >> x;
            if (a == 0)
                v.push_back(x);
            else {
                pq.push({ v.size(), x });
            }
        }
        int x = 0;
        auto y = make_pair(0, 0);
        while (pq.top() != y) {
            auto top = pq.top();
            x ^= top.second;
            pq.pop();
            while (pq.top().first == top.first) {
                x ^= pq.top().second;
                pq.pop();
            }
            for (int i = top.first - 1; i >= pq.top().first;
                 i--) {
                v[i] ^= x;
            }
        }
        sort(v.begin(), v.end());
        for (auto a : v)
            cout << a << " ";
    }
}


8 12 13 14

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!