loading...

Daily Coding Challenge #60

wingkwong profile image Wing-Kam WONG ・2 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.


/*
AIsing Programming Contest 2020 - A - Number of Multiples
https://atcoder.jp/contests/aising2020/tasks/aising2020_a
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
int main()  
{ 
    FAST_INP;
    int l,r,d;
    cin >> l >> r>> d;
    int ans=0;
    // check if i can be divided by d without a remainder 
    for(int i=l;i<=r;i++){
        if(i%d==0) ans++;
    }
    cout << ans;
    return 0;
} 

/*
AIsing Programming Contest 2020 - B - An Odd Problem
https://atcoder.jp/contests/aising2020/tasks/aising2020_b
*/

#include <bits/stdc++.h>
using namespace std; 

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
int main()  
{ 
    FAST_INP;
    int n;
    cin >> n;
    int ans=0;
    for(int i=1;i<=n;i++) {
        int a;
        cin >> a;
//      check if the assigned number, i, is odd, and
//      check if the written integer is odd
        if((i&1)&&(a&1)) ans++;
    }
    cout << ans;
    return 0;
} 

/*
AIsing Programming Contest 2020 - C - XYZ Triplets
https://atcoder.jp/contests/aising2020/tasks/aising2020_c
*/

#include <bits/stdc++.h>
using namespace std; 

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

int main()  
{ 
    FAST_INP;
    int n;
    cin >> n;
    unordered_map<int,int> ans(n);
    // pre-build the ans map
    // note that N<= 10^4
    for(int x=1;x<=105;x++){
        for(int y=1;y<=105;y++){
            for(int z=1;z<=105;z++){
                int k = (x*x)+(y*y)+(z*z)+(x*y)+(y*z)+(z*x);
                if( k < 10005 ) {
                    ans[k]++;
                } 
            }
        }
    }
    for(int i=1;i<=n;i++) cout << ans[i] << "\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 🏆

Discussion

pic
Editor guide