loading...

Daily HackerRank Challenge - Day 3

wingkwong profile image Wing-Kam Updated on ・3 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++.


Staircase

Consider a staircase of size n=4 :

   #
  ##
 ###
####

Observe that its base and height are both equal to n, and the image is drawn using n symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Sample Input

6 

Sample Output


     #
    ##
   ###
  ####
 #####
######

Given that n=4, we can list out how many spaces s and hash symbols h are required.

   #  s: 3 - h:1 
  ##  s: 2 - h:2
 ###  s: 1 - h:3
####  s: 0 - h:4

The pattern can be easily observed. As n increases, s decreases while h increases. And we can notice that n-i-1 spaces and i+1 hash symbols for row i starting from 0 are needed. We can utilise string initialisation to resolve this question and concatenate the spaces and hash symbols.

Final Solution

int n;

int main() {
    FAST_INP;

    cin >> n;
    REP(i,n){
        string s(n-i-1,' ');    
        string h(i+1,'#');
        cout << s << h << "\n";
    }
    return 0;
}

Time Conversion

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.

Sample Input

07:05:45PM

Sample Output

19:05:45

First we notice that the first two characters 07 will be displayed as 19 based on the last two characters PM which will be removed in the output. Hence, we can use substr to get these two variables first.

Take 2 characters from index 0 and convert it to an integer.

hh=stoi(s.substr(0,2));

Take 2 characters from index 8

ap=s.substr(8,2);

Here it shows each character with corresponding index

s:07:05:45PM    
i:0123456789

If ap is AM, there is a case that the time is 12:00:00AM. We need to output it as 00:00:00. Hence we can check if hh is 12, the first two characters of the output would be 00. Since ap is AM, hh must between 00 to 12. However, we use stoi to convert it an integer. If hh is below 10, the leading 0 will be missing. Therefore, we need to add it back for such case.

If ap is PM, similarly we add the missing leading 0 for hh<10 cases. Otherwise, we can display hh+12.

We need to append s.substr(2,6) to the first characters to form the answer.

Final Solution

int hh;
string s,ap,r;

int main() {
    FAST_INP;

    cin >> s;
    hh=stoi(s.substr(0,2));
    ap=s.substr(8,2);
    r=s.substr(2,6);
    if(ap=="AM") {
        if(hh==12) cout << "00" << r;
        else cout << (hh<10?"0":"") << hh << r;
    }
    else if(ap=="PM") {
        if(hh==12) cout << (hh<10?"0":"") << hh << r;
        else cout << (hh+12) << r;
    }
    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