Viral Advertising

HackerLand Enterprise is adopting a new viral advertising strategy. When they launch a new product, they advertise it to exactly 5 people on social media.

On the first day, half of those 5 people (i.e., ) like the advertisement and each share it with 3 of their friends. At the beginning of the second day,  people receive the advertisement.

Each day, the recipients like the advertisement and will share it with 3 friends on the following day. Assuming nobody receives the advertisement twice, determine how many people have liked the ad by the end of a given day, beginning with launch day as day 1.


Example:

Input:  n=5
Output: 24

Approach

Java


public class ViralAdvertising {
    public static void main(String[] args) {
        int n = 5;
        System.out.println(viralAdvertising(n));
    }

    static int viralAdvertising(int n) {
        int sum = 0, x = 5, y = 3;
        for (int i = 1; i <= n; i++) {
            x = x / 2;
            sum += x;
            x = x * y;
        }
        return sum;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int viralAdvertising(int n)
{
    int sum = 0x = 5y = 3;
    for (int i = 1i <= ni++)
    {
        x = x / 2;
        sum += x;
        x = x * y;
    }
    return sum;
}

int main()
{
    int n = 5;
    cout << viralAdvertising(n);
    return 0;
}


No comments:

Post a Comment