# Daily HackerRank Challenge - Day 3 Wing-Kam Updated on ・3 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++.

# 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)

### Discussion   