Hackerrank - Viral Advertising Solution
1 min read

Hackerrank - Viral Advertising Solution

Hackerrank - Viral Advertising Solution

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

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

Each day,  of the recipients like the advertisement and will share it with  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 .

For example, assume you want to know how many have liked the ad by the end of the  day.Day Shared Liked Cumulative1      5     2       22      6     3       53      9     4       94     12     6      155     18     9      24

The cumulative number of likes is .

Function Description

Complete the viralAdvertising function in the editor below. It should return the cumulative number of people who have liked the ad at a given time.

viralAdvertising has the following parameter(s):

  • n: the integer number of days

Input Format

A single integer, , denoting a number of days.


Output Format

Print the number of people who liked the advertisement during the first  days.

Sample Input


Sample Output



This example is depicted in the following diagram:

strange ad.png

people liked the advertisement on the first day,  people liked the advertisement on the second day and  people liked the advertisement on the third day, so the answer is .

Solution in Python

a = 5
b = 0
for i in range(1,int(input())+1):
    b += a//2
    a  = a//2*3

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.