loading...

Daily Coding Challenge #50

wingkwong profile image wkw ・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.


/*
Codeforces Round #653 (Div. 3) - A. Required Remainder
https://codeforces.com/contest/1374/problem/A
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

void solve(){
    int x,y,n;
    cin >>x>>y>>n;
    // if n%x is y, print n 
    if(n%x==y) cout << n << "\n";
    // if k%x is less than y, e.g. 12345%7=4 but we need 5
    // in this case, the max k would be in the previous cycle 
    // n - (n%x) - (x-y) 
    // (n%x) : reach to a point where the remainder is 0
    // (x-y) : reach to the target y
    else if(n%x<y) cout << (n-(n%x)-(x-y)) << "\n";
    // if it is still in the cycle, simply deduct n by ((n%x)-y)
    else cout << (n-((n%x)-y)) << "\n";
} 

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


/*
Codeforces Round #653 (Div. 3) - B. Multiply by 2, divide by 6
https://codeforces.com/contest/1374/problem/B
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int solve(int n){
    int c2=0,c3=0;
    while(n%3==0){
        c3++;
        n/=3;
    }
    while(n%2==0){
        c2++;
        n/=2;
    }
    // -1 cases
    if(n>1||c2>c3) return -1;
    // c3-c2: how many times to multiply by 2
    // c3: how many times to divide by 6
    return c3-c2+c3;
}

int main()  
{ 
    FAST_INP;
    int n,t;
    cin >> t;
    while(t--){
        cin >> n;
        cout << solve(n) << endl;
    }
    return 0;
} 


/*
Codeforces Round #653 (Div. 3) - C. Move Brackets
https://codeforces.com/contest/1374/problem/C
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int solve(string s){
    int l=0,ans=0;
    for(char c:s){
        if(c=='('){
            l++;
        } else{
            l--;
            if(l<0) 
                // if l s less than 0, 
                // it means this bracket needs to be moved
                l=0;
                ans++;
            }
        }
    }
    return ans;
}

int main()  
{ 
    FAST_INP;
    int n,t;
    string s;
    cin >> t;
    while(t--){
        cin >> n >> s;
        cout << solve(s) << "\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