Viral Advertising


Problem Statement :


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)*3 = 2*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.
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



Solution :



title-img


                            Solution in C :

python 3  :

people = 5
total = 0
n = int(input())

for i in range(n):
	receive = int(people/2)
	total+=receive
	people = receive*3
print(total)










Java  :

import java.io.*;
import java.util.*;

public class Solution {

    public static final int INITIAL_AMOUNT_OF_PEOPLE = 5;

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            int currentAmount = INITIAL_AMOUNT_OF_PEOPLE;
            int totalNumber = 0;
            for (int i = 0; i < n; i++) {
                currentAmount = currentAmount/2;
                totalNumber += currentAmount;
                currentAmount *= 3;
            }
            System.out.println(totalNumber);
        }
    }
}












C++  :

#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;
}










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;
}
                        








View More Similar Problems

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →