loading...

Daily HackerRank Challenge - Day 16

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++.


Minimum Absolute Difference in an Array

image

Sample Input

3
3 -7 0

Sample Output

3

The first approach is to loop through the array between 0..n-1, 1..n-2, and 2..n-3, and so on. However, this approach is not ideal because we are finding all the differences. Instead,
We can sort the array first because the values that are closest to one another produces the least difference such as (10,8) < (10,1). We can use C++ STL to solve this problem by calculating the difference between each adjacent element, and returning the minimum value of the array.

Final Solution

int n;

int comp(int x, int y){
    return abs(x-y);
}

int main()  
{ 
    FAST_INP;
    cin >> n;
    vi arr(n);
    vi result(n);
    REP(i,n) cin >> arr[i];
    sort(arr.begin(), arr.end());
    adjacent_difference(arr.begin(), arr.end(), result.begin(), comp);
    cout << *min_element(result.begin()+1, result.end());
    return 0; 
} 

Reference:


Marc's Cakewalk

image

Sample Input

3
1 3 2

Sample Output

11

Since we need the minimum value, so we can sort the array in a descending order and apply the formula sum = sum + 2^i*v[i] to find out the final sum value.

sort(v.rbegin(),v.rend());
REP(i,n) sum+=pow(2,i)*v[i];
cout<<sum;

Final Solution

int n;
ll sum=0;

int main()  
{ 
    FAST_INP;
    cin>>n;
    vi v(n);
    REP(i,n) cin>>v[i];
    sort(v.rbegin(),v.rend());
    REP(i,n) sum+=pow(2,i)*v[i];
    cout<<sum;
    return 0; 
} 

Luck Balance

image

We sort the array in a descending order and assume Lena loses all non-important competition so that we can have maximum luck. If it it an important contest, we add T[i] to her luck and deduct k by 1 because losing a contest means increasing her luck. The more L[i] it provides , the more luck she will get. If she cannot lose anymore, deduct the luck by T[i].

Final Solution

int n,k,ans=0;

int main()  
{ 
    FAST_INP;
    cin>>n>>k;
    vector<vector<int>> c(n);
    for (int i=0; i<n; i++) {
        c[i].resize(2);
        for (int j = 0; j < 2; j++) cin >> c[i][j];
    }
    sort(c.rbegin(),c.rend());
    REP(i,n){
        if(!c[i][1]){
            // not important 
            ans+=c[i][0];
        }else{
            if(k){
                ans+=c[i][0]; // lose
                k--;
            }
            else ans-=c[i][0];    // win
        }
    }
    cout << ans;
    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