loading...

Daily Coding Challenge #82

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


/*
Educational Codeforces Round 92 (Rated for Div. 2) - A. LCM Problem
https://codeforces.com/contest/1389/problem/A
*/

#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 l, r, t;
    cin>>t;
    while(t--){
         cin>>l>>r;
         // if the upper number is multiple of the lower number
         // and the upper number is within [l,r]
         // then the answer exists 
         // e.g. LCM(a,b) where b is a*2 = b
         if(2*l<=r) cout<<l<<" "<<2*l<<endl;
         else cout<<-1<<" "<<-1<<endl;
    }
    return 0;
}  

/*
Educational Codeforces Round 92 (Rated for Div. 2) - C. Good String
https://codeforces.com/contest/1389/problem/C
*/

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

void solve() {
    // tkt1t2...tk-1 and t2t3...tkt1
    // so tk=t2, t1=t3, t2=t4 ...
    // even length: ABABABAB
    // odd length: AAAAA
    // brute force all possible combinations
    string s;
    cin >> s;
    int ans = 0;
    for (char i = '0'; i <= '9'; i++) {
        for (char j = '0'; j <= '9'; j++) {
            char x=i, y=j;
            int cnt = 0;
            for(char c:s){
                if(c==x){
                    cnt++;
                    swap(x,y);
                }
            }
            if(i==j) ans=max(ans,cnt);
            // if the cnt is odd and i!=j, cnt=cnt--;
            else ans=max(ans,cnt&~1);
        }
    }
    cout << (s.size() - ans) << endl;
}

int main() {
    FAST_INP;
    int t;
    string s;
    cin >> t;
    while (t--) {
        solve();
    }
    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