bitmasks brute force *800

Please click on ads to support us..

Python Code:

x = int(input())
  
for jj in range(x):
    n = int(input())
    if n == 1:print(3)
    else:
        if n%2 == 1:
            print(1)
        else:
            p = 0
            k = n
            while n%2==0:
                p += 1
                n //= 2
            t = 2**p
            if t == k:
                print(t+1)
            else:
                print(t)

C++ Code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        if (n % 2)
    { // odd
        if (n > 1)
            cout << "1\n";
        else
            cout << "3\n";
    }
    else
    {
        if (!(n & (n - 1)))
        { // power of two
            cout << n + 1 << endl;
        }
        else
        { // we are leaving the most sig. bit and unsetting all bits.
            cout << (n & ~(n - 1)) << endl;
        }
    }
    }
}


Comments

Submit
0 Comments
More Questions

505B - Mr Kitayuta's Colorful Graph
1324D - Pair of Topics
157B - Trace
34C - Page Numbers
279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings