loading...

Daily HackerRank Challenge - Day 17

wingkwong profile image Wing-Kam ・2 min read

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.


Repeated String

image

Sample Input

aba
10

Sample Output

7

Explanation
The first n=10 letters of the infinite string are abaabaabaa. Because there are 7 a's, we print 7 on a new line.

If s only contains letter a, we can directly print n.

if(s.size()==1 && s=="a") cout << n;

otherwise, we calculate the number of a in a given string.

c=count(s.begin(),s.end(),'a')

We then calculate how manys times s needs to repeat without exceeding n.

r=n/s.size()

Take the sample input as an example, there are 2 a's and n is 10. It will repeat three times leaving a single a at the end.

abaabaaba a

We also need to take care of the last part. To find out how many a's there, we can use n%s.size(). In this case, it is 1. Hence, we can calculate the number of a from s.begin() to s.begin()+n%s.size().

count(s.begin(),s.begin()+(n%s.size()),'a')

Final Solution


string s;
ll n;

int main()  
{ 
    FAST_INP;
    cin >> s >> n;
    if(s.size()==1 && s=="a") cout << n;
    else cout << count(s.begin(),s.end(),'a')*(n/s.size())+count(s.begin(),s.begin()+(n%s.size()),'a');

    return 0; 
} 

Ice Cream Parlor

image

Sample Input

2
4
5
1 4 5 3 2
4
4
2 2 4 3

Sample Output

1 4
1 2

It's like Two Sum question. We insert v to a map if m-v is not found or print the result if it is found.

Take the first case in the sample input as an example, we are looking for those two numbers which add each other equal to 4.

k id
----
1 0 
4 1 
5 2

By the time we check 3, we found that m-v (4-3) which exists in the map. Hence, mp[m-v] and the current value v are the answer.

Final Solution


int t,m,n,v;
int main()  
{ 
    FAST_INP;

    cin >> t;
    TC(t){
        cin >> m >> n;
        multimap<int,int> mp; 
        REP(i,n){
            cin >> v;
            auto it=mp.find(m-v);
            // cout << "Finding " << (m-v) << "\n";
            if(it==mp.end()){
                // cout << "Not found. Inserting " << v << "\n";
                mp.insert({v,i});
            } else {
                // cout << "Found";
                cout << it->second+1 << " " << i+1 << "\n";
            }
        }
    }

    return 0; 
} 

Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide