loading...

Daily HackerRank Challenge - Day 12

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


Migratory Birds

You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.

For example, assume your bird sightings are of types arr=[1,1,2,2,3]. There are two each of types 1 and 2, and one sighting of type 3. Pick the lower of the two types seen twice: type 1.

Sample Input

6
1 4 4 4 5 3

Sample Output

4

Loop through the array to count each type.

cin >> v;
a[v-1]++; // 0-base

We need a variable max to store the maximum value.

int max=0;
if(a[v-1]>=max) max=a[v-1];

If several types have the same frequency, show the lowest one. Therefore, we need another variable id and update it only max is updated or the input value is less than id.

if(a[v-1]>max||v<id) id=v;
6
1 4 4 4 5 3
-----------------------------
1: 1
2: 0
3: 1
4: 3
5: 1
-----------------------------
1 - max: 1, id:1
4 - max: 1, id:1
4 - max: 2, id:4
4 - max: 3, id:4
5 - max: 3, id:4
3 - max: 3, id:4
-----------------------------
ans = 4

Final Solution

int n,v,id=5,a[5]={0};
int main()  
{ 
    FAST_INP;
    cin >> n;
    int max=0;
    REP(i,n){
        cin >> v;
        a[v-1]++;
        if(a[v-1]>=max){
            if(a[v-1]>max||v<id) id=v;
            max=a[v-1];
        }
    }        
    cout << id << "\n";
    return 0; 
} 

Find Digits

An integer d is a divisor of an integer n if the remainder of n/d = 0.

Given an integer, for each digit that makes up the integer determine whether it is a divisor. Count the number of divisors occurring within the integer.

Note: Each digit is considered to be unique, so each occurrence of the same digit should be counted (e.g. for n=111 , 1 is a divisor of 111 each time it occurs so the answer is 3).

Sample Input

2
12
1012

Sample Output

2
3

Explanation

The number 12 is broken into two digits, 1 and 2. When 12 is divided by either of those two digits, the remainder is 0 so they are both divisors.

The number 1012 is broken into four digits,1,0,1 and 2. 1012 is evenly divisible by its digits 1,1, and 2, but it is not divisible by 0 as division by zero is undefined.

Store the input to mm first because n will be updated

nn=n;

For this kind of questions, we have below structure

while(n){
    // get the last digit
    // do something
    // divide n by 10
}

Get the last digit m of the input n

m=n%10;

Divide the input by m to check if the remainder is 0. If so, increase ans by 1. We only do it when m is non-zero.

if(m&&nn%m==0) ans++;

Update n. Simple divide by 10 to handle the next digit for the next loop

n/=10;
1012 
n%10 -> 2 
1012%2 = 0 // increase ans by 1
101 // divided by 10
----
n%10 -> 1 
101%1 = 0 // increase any by 1
10 // divided by 10
----
n%10 -> 0 // skip
1 // divided by 10
----
n%10 -> 1 
1%1 = 0 // increase any by 1
----
ans = 3 

Final Solution

int t,n,ans,m,nn;

int main()  
{ 
    FAST_INP;
    cin>>t;

    REP(i,t){
        ans=0;
        cin>>n;
        nn=n;
        while(n){
            m=n%10;
            if(m&&nn%m==0) ans++;
            n/=10;
        }
        cout << ans << "\n";
    }
    return 0;
}

Complete Code

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 Apr 24 by:

wingkwong profile

Wing-Kam

@wingkwong

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

Discussion

markdown guide