loading...

Daily Coding Challenge #67

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


/*
Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:

Input: 2.00000, 10
Output: 1024.00000
Example 2:

Input: 2.10000, 3
Output: 9.26100
Example 3:

Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:

-100.0 < x < 100.0
n is a 32-bit signed integer, within the range [−231, 231 − 1]
*/

class Solution {
public:
    double myPow(double x, int n) {
        // recusive approach
        double ans=1;
        bool inv=n<0;
        while(n!=0){
            // if n is odd, a^n can be seen as a^(n/2) * a^(n/2) * a
            if(n&1) ans*=x;
            // if n is even, a^n can be seen as a^(n/2) * a^(n/2)
            x*=x;
            n/=2;
        };
        return inv?1/ans:ans;
    }
};

/*
Ada King - Problem Code: ADAKING
https://www.codechef.com/JULY20B/problems/ADAKING
*/

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

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

void solve(int k){
    // a bit hacky
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            k--;
            // place the king at (0,0) 
            if(i==0&&j==0){
                cout << 'O';
                continue;
            }
            // place . k times 
            if(k>=0) cout << '.';
            // place X on the rest of the cells
            else cout << 'X';
        }
        cout << "\n";
    }   
}

int main()  
{ 
    FAST_INP;
    int t, k;
    cin >> t;
    while(t--){
        cin >> k;
        solve(k);
    }
    return 0;
} 

/*
Chef and Strings - Problem Code: CHEFSTR1
https://www.codechef.com/JULY20B/problems/CHEFSTR1/
*/

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

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

int main()  
{ 
    FAST_INP;
    long long t,n;
    cin >> t;
    while(t--){
        cin >> n;
        vector<int> s(n);
        for(int i=0;i<n;i++) cin >> s[i];
        long long ans=0;
        for(int i=1;i<n;i++) {
            // sum up the number of strings to skip
            ans+=abs(s[i]-s[i-1])-1;
        }
        cout << ans << "\n";
    }
    return 0;
} 

/*
Chef and Card Game - Problem Code: CRDGAME
https://www.codechef.com/viewsolution/34951273
*/

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

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

int ds(int val){
    // calculate digit sum
    int sum=0;
    while(val){
        sum+=val%10;
        val/=10;
    }
    return sum;
}

int main()  
{ 
    FAST_INP;
    int t,n,a,b,c,m;
    cin >> t;
    while(t--){
        cin >> n;
        c=0,m=0;
        for(int i=0;i<n;i++){
            cin >> a >> b;
            // get the digit sum
            int aa = ds(a), bb = ds(b);
            if(aa>bb){
                // Chef who draws the card with higher power wins this round and gets a point
                c++;
            }else if(aa==bb) {
                // If the powers of both players' cards are equal then they get 1 point each.
                c++;
                m++;
            }else{
                // Morty who draws the card with higher power wins this round and gets a point
                m++;
            }
        }

        if(c>m) cout << 0 << " " << c;
        else if(c<m) cout << 1 << " " << m;
        else cout << 2 << " " << c;
        cout << "\n";

    }
    return 0;
} 

/*
Missing a Point - Problem Code: PTMSSNG
https://www.codechef.com/JULY20B/problems/PTMSSNG
*/

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

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

int main()  
{ 
    FAST_INP;
    int t,n,x,y;
    cin >> t;
    while(t--){
        int ansx=0, ansy=0;
        cin >> n;
        for(int i=0;i<4*n-1;i++){
            cin >> x >> y;
            // both x-axis and y-axis missing one point
            // use XOR to find out 
            ansx^=x;
            ansy^=y;
        }
        cout << ansx << " " << ansy << "\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