loading...

Daily HackerRank Challenge - Day 2

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

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
 

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

 

Nope. I randomly picked the questions.