# Daily HackerRank Challenge - Day 2

Wing-Kam Updated on ・3 min read

Daily HackerRank Challenge (30 Part Series)

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

# Birthday Cake Candles

You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.

For example, if your niece is turning 4 years old, and the cake will have candles of height 4, 4, 1, 3, she will be able to blow out 2 candles successfully, since the tallest candles are of height 4 and there are such candles.

Sample Input

4
3 2 1 3


Sample Output

2


Read the input n and loop n times.
Within the loop, read another input c.
Check if the input is greater than the tallest height t.

• If so, set the input cc as the tallest. Reset the counter s.
• If the input is same as the tallest, increase s by 1
• If the input is lower, do nothing

Final Solution

int n,c,t=0,s=0;

int main() {
FAST_INP;

cin >> n;
REP(i,n){
cin >> c;
if(c>t) {t=c; s=1;}
else if(c==t) s++;
}
cout << s;
return 0;
}


# Mini-Max Sum

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

For example, arr=[1,3,5,7,9]. Our minimum sum is 1+3+5+7=16 and our maximum sum is 3+5+7+9=24 . We would print

16 24


Sample Input

1 2 3 4 5


Sample Output

10 14


From the example, we can see that pattern. The minimum sum is the sum of the array minus the maximum value, while the maximum sun is that minus the minimum value.

The input is a single line of five space-separated integers. Hence, we can first declare int a[5]. Loop 5 times to read each value and find out the minimum value minV and maximum value maxV. At the same time, we sum up each value to get the sum of the array.

Use cout to output the minimum sum sum-maxV and the maximum sum sum-minV.

Final Solution

int a[5];
int minV,maxV;
ll sum=0;

int main() {
FAST_INP;

REP(i,5){
cin >> a[i];
if(i==0)minV=maxV=a[i];
if(a[i]<minV)minV=a[i];
if(a[i]>maxV)maxV=a[i];
sum+=a[i];
}
cout << sum-maxV << " " << sum-minV;
return 0;
}


# Plus Minus

Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.

Sample Input

6
-4 3 -9 0 4 1


Sample Output

0.500000
0.333333
0.166667


The output contains three lines

A decimal representing of the fraction of positive numbers in the array compared to its size.
A decimal representing of the fraction of negative numbers in the array compared to its size.
A decimal representing of the fraction of zeros in the array compared to its size.


Read the input t and loop t times. In each loop, read v to identify whether it is possible, negative or zero.

Each line of output needs to be rounded to six decimals. Use printf("%.6f\n",value); to display the value.

Final Solution:

int t,v;
float p=0,n=0,z=0;

int main()
{
FAST_INP;

cin >> t;
REP(i,t){
cin >> v;
if(v>0)p++;
else if(v<0)n++;
else if(v==0)z++;
}

printf("%.6f\n",p/t);
printf("%.6f\n",n/t);
printf("%.6f\n",z/t);

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.

### Discussion

Is this an official challenge just like leetcode is doing a 30 days challenge?

Nope. I randomly picked the questions.