loading...

Daily Coding Challenge #85

wingkwong profile image Wing-Kam ・1 min read

About

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.


/*
LeetCode - Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true
Example 2:

Input: 5
Output: false
Follow up: Could you solve it without loops/recursion?
*/

class Solution {
public:
    bool isPowerOfFour(int num) {
        // 4: 100
        // 16: 10000
        // obversation: 
        // count of 1s is 1 and the number of trailing zeros is even
        return __builtin_popcount(num)==1 && // only 1 bit is set
              (__builtin_ctz(num)&1)==0;    // with even trailing zeros 
    }
};

/*
Hello 2019 - B. Petr and a Combination Lock
https://codeforces.com/problemset/problem/1097/B
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
typedef long long ll; 
const ll MOD = 1000000007;

int main()  
{ 
    FAST_INP;
    int n;
    cin >> n;
    vector<int> a(n);
    for(int &x:a) cin >> x;
    for(int mask=0;mask<(1<<n);mask++){
        int ans=0;
        for(int i=0;i<n;i++){
            if(mask&(1<<i)) ans+=a[i];
            else ans-=a[i];
        }
        if(ans%360==0) {
            cout << "YES\n";
            return 0;
        }
    } 
    cout << "NO\n";
    return 0;
}  

The source code is available in corresponding repo below. Star and watch for timely updates!

GitHub logo wingkwong / leetcode

🏆 A Collection of my LeetCode Solutions with Explanations 🏆

GitHub logo wingkwong / hackerrank

🏆 A Collection of my HackerRank Solutions with Explanations 🏆

GitHub logo wingkwong / codeforces

🏆 A Collection of my Codeforces Solutions with Explanations 🏆

GitHub logo wingkwong / atcoder

🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide