18
Jan
2020

Hackerrank Viral Advertising (C++)

C++ :

int viralAdvertising(int n) {
    int totalLikes = 0, shares = 5, likes = 0; 

    for(int i = 0; i < n; i++){
        likes = floor(shares / 2);
        totalLikes += likes;
        shares = likes * 3; 
    }

    return totalLikes;
}

Explanation:

At first glance, this question feels like a pyramid scheme. The for loop in this case does exactly like what the question says: It first gets the number of likes for the day, floor it by (number of shares / 2), add the number of likes for the day to totalLikes, then calculate the number of shares for the next day with number of likes * 3. Pretty simple question, really.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *