#### ByBrokenprogrammers

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Viral Advertising Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

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(5/2) = 2) like the advertisement and each shares it with 3 of their friends. At the beginning of the second day, floor(5/2) x 3 = 2 x 3 = 6 people receive the advertisement.

Each day, floor(recipients/2) of 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

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.

• 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

Explanation

This example is depicted in the following diagram:

### Viral Advertising 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);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

### Viral Advertising Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
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 Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long cur = 5, ans = 0;
while(n-- > 0){
cur /= 2;
ans += cur;
cur *= 3;
}
System.out.println(ans);
}
}

### Viral Advertising 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 Solution using JavaScript

function processData(input) {
var n = parseInt(input);
var k = 5,sum=0;
for(var i=0;i<n;i++){
k=(Math.floor(k/2))*3;
sum = sum + (k/3);
}
console.log(sum);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});

### Viral Advertising Solution in Scala

object Solution {
def main(args: Array[String]) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/val input = io.Source.stdin.getLines()
val n = input.next().toInt
import scala.annotation.tailrec
@tailrec def floor(day: Int, friends: Int, sum: Int ): Unit = {
if (day == 0){
println(sum)
} else {
floor(day - 1, (friends >> 1) * 3, sum + (friends >> 1))
}
}
floor(n, 5, 0)
}
}

### Viral Advertising Solution in Pascal

uses crt;
var n,i:longint;
f,sum:array[1..100] of int64;
begin
f[1]:=2;
sum[1]:=2;
for i:=2 to n do
begin
f[i]:=(f[i-1]*3) div 2;
sum[i]:=sum[i-1]+f[i];
end;
writeln(sum[n]);
end.

Disclaimer: This problem (Viral Advertising) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.