Viral Advertising HackerRank Solution in C, C++, Java, Python

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.,floor ) like the advertisement and each shares it with  3 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 .

Example

.n=5

Day Shared Liked Cumulative

1      5     2       2

2      6     3       5

3      9     4       9

4     12     6      15

5     18     9      24

 

The progression is shown above. The cumulative number of likes on the 5th day is 24.

Function Description

Complete the viralAdvertising function in the editor below.

viralAdvertising has the following parameter(s):

  • int n: the day number to report

Returns

  • int: the cumulative likes at that day

Input Format

A single integer,n , the day number.

Constraints

  • 1<=n<=50

Sample Input

3

Sample Output

9

 

Viral Advertising HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i=0,j,k,l,m,n,t;
    scanf("%d",&n);
    k=5;
    for(i=0;i<n;i++)
        {
        j=k/2;
        k=j*3;
        m=m+j;
    }
    printf("%d",m);
       
    int n;
    cin>>n;
    
    int m = 5;
    int total;
    for(int i=0; i<n; ++i){
        m = m/2;
        total += m;
        m *= 3;
    }
    
    cout<<total<<endl;
    return 0;
}

 

Viral Advertising HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    
    
    public static int viral(int day) {
        int numReceived = 0;
        int num = 5;
        while (day >= 1) {
            int newNum = num / 2;
            numReceived += newNum;
            num = newNum * 3;
            day--;
        }
        return numReceived;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int days = in.nextInt();
        System.out.println(viral(days));
    }
}

 

Viral Advertising HackerRank Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(raw_input())
p=2
a=2
for x in range(1,n):
    p+=a*3//2
    a=a*3//2
print p

 

Viral Advertising HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Solution
    {
        static void Main(string[] args)
        {
            var n = Convert.ToInt32(Console.ReadLine());

            var result = 0;

            var currentNum = 5;

            for (int i = 0; i < n; i++)
            {
                currentNum = currentNum / 2;
                result += currentNum;
                currentNum *= 3;
            }

            Console.WriteLine(result);
        }
    }
}

 

Attempt Viral Advertisement HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/strange-advertising/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/save-the-prisoner-hackerrank-solution/

Leave a Comment