loading...

Daily Coding Challenge #37

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 #650 (Div. 3) - A. Short Substrings
https://codeforces.com/contest/1367/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;
    int t; 
    string s;
    cin >> t;
    while(t--){
        cin >> s;
        // the first two characters must be same 
        string ans=s.substr(0,2);
        // travese the rest of the string
        // skip 1 character and print it
        for(int i=3;i<=s.size();i+=2){
            ans+=s[i];
        }        
        cout << ans << "\n";
    }
    return 0;
} 

/*
Codeforces Round #650 (Div. 3) - B. Even Array
https://codeforces.com/contest/1367/problem/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 t,n;
    cin >> t;
    while(t--){
        cin >> n;
        int odd=0,odd1=0,even=0,even1=0,ans=0,a;
        for(int i=0;i<n;i++){
            cin >> a;
            if(a%2==i%2) continue;
            // calculate how many even & odd numbers for the input
            if(a%2==0) even++;
            else odd++;
            // calculate how many even & odd numbers for the index
            if(i%2==0) even1++;
            else odd1++;
        }
        // if they are equal, it means it fits the place 
        // the move would be (odd+even)/2
        cout << (even==even1&&odd==odd1?(odd+even)/2:-1) << "\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