• + 0 comments

    Theory

    Interpretation: The problem asks to calculate the total number of likes after days using the formula. Each day, people like the advertisement. Each person who likes the advertisement then shares it with other people.

    Abstract: At day , people like the advertisement. Then those share the post with other people each: . Therefore, at the beginning of the second day, people recieve the advertisement. Following this pattern for days will yield the total number of people who liked the advertisement.

    Approach: Hold the number of people on day , in a variable p. Hold the total number of likes after days in a variable t. Likes are calculated by , which is added to the total likes t. Then, the number of people who liked the post is changed to the number of people who liked the advertisement this round.

    Code

    int  viralAdvertising(int  n)  {
          int  p  =  5;
          int  t  =  0;
          for  (int  i  =  0;  i  <  n;  ++i)  {
                int  l  =  floor(p  /  2);
                t  +=  l;
                p  =  l  *  3;
          }
          return  t;
    }