1299A - Anu Has a Function - CodeForces Solution


brute force greedy math *1500

Please click on ads to support us..

Python Code:

n = int(input())
a = list(map(int, input().split()))
r = 0

for i in range(29, -1, -1):
    c = 0
    for j in a:
        if (j >> i) & 1:
            c += 1
            r = j
    if c == 1:
        break

a.remove(r)

print(r, ' '.join(map(str, a)))

num_inp=lambda: int(input())
arr_inp=lambda: list(map(int,input().split()))
sp_inp=lambda: map(int,input().split())
str_inp=lambda:input()

C++ Code:

#include <bits/stdc++.h>

#define fs first
#define se second
#define pb push_back
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define all(x) (x).begin(), (x).end()
#define mk make_pair
#define endl '\n'
// #define double long double
#define int long long
using namespace std;

typedef long long LL;
const int N = 2e5 + 10, mod = 1e9 + 7, M = 2 * N;
const double eps = 1e-5;
const LL INF = 1e15;
typedef pair<int, int> PII;

int g[N];
vector<int> v[50];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> g[i];

        for (int j = 31; j >= 0; j--)
        {
            if ((g[i] >> j) & 1)
            {
                v[j].pb(i);
            }
        }
    }
    int flag=0;

    for(int i=30;i>=0;i--)
    {
        if(v[i].size()==1)
        {
            cout<<g[v[i][0]]<<' ';
            flag=v[i][0];
            break;
        }
    }

    if(!flag) 
    {
        for(int i=1;i<=n;i++) cout<<g[i]<<' ';
    }
    else 
    {
        for(int i=1;i<=n;i++)
        {
            if(i!=flag) cout<<g[i]<<' ';
        }
    } 
    return 0;
}


Comments

Submit
0 Comments
More Questions

25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals