# Daily HackerRank Challenge - Day 17

Wing-Kam ãƒ»2 min read

Daily HackerRank Challenge (30 Part Series)

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

# Repeated String

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

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)

Posted on by:

### Wing-Kam

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