Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Viral Advertising Problem Solution

HackerRank Viral Advertising Problem Solution

Posted on April 15, 2023April 15, 2023 By Yashwant Parihar No Comments on HackerRank Viral Advertising Problem Solution

In this post, we will solve HackerRank Viral Advertising Problem. 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 2 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

Sample Input

3

Sample Output

9

Explanation

This example is depicted in the following diagram:

2 people liked the advertisement on the first day, 3 people liked the advertisement on the second day and 4 people liked the advertisement on the third day, so the answer is 2 + 3 + 4 = 9.

HackerRank Viral Advertising Problem Solution
HackerRank Viral Advertising Problem Solution

Table of Contents

  • Viral Advertising C Solution
  • Viral Advertising C++ Solution
  • Viral Advertising C Sharp Solution
  • Viral Advertising Java Solution
  • Viral Advertising Javascript Solution
  • Viral Advertising Python Solution

Viral Advertising C Solution

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

int main() {
    int in, total=2, jour, anciens=2, i;
    scanf("%d", &in); 
    for(jour=2;jour<in+1;jour++){
        anciens = (3*anciens)/2;
        total += anciens;
    }
    printf("%d", total);
    return 0;
}

Viral Advertising C++ Solution

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <cassert>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<pair<int, int>>> vvpii;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int f = 2;
    int ans = 2;
    for (int i = 1; i < n; i++) {
        f = 3*f/2;
        ans += f;
    }
    cout << ans << endl;

    return 0;
}

Viral Advertising C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = int.Parse(Console.ReadLine());
        int m = 5/2;
        int sum = m;
        for(int i=1;i<n;i++)
            {
            m = m*3/2;
            sum+=m;
        }
        Console.WriteLine(sum);
    }
}

Viral Advertising Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'viralAdvertising' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER n as parameter.
     */

    public static int viralAdvertising(int n) {
    // Write your code here
        int cummulative = 2;
        int shared = 5;
        int liked = 2;
        int day = 1;
        
        while (day < n) {
            shared = Math.floorDiv(shared,2) * 3;
            liked = Math.floorDiv(shared,2);
            cummulative += liked;
            day++;
        }
        return cummulative;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        int result = Result.viralAdvertising(n);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Viral Advertising Javascript Solution

function processData(input) {
    const n = Number(input);
    let people = 5;
    let result = 0;
    for(var i=0; i<n; i++) {
        result += Math.floor(people / 2);
        people = Math.floor(people / 2) * 3;
    }
    console.log(result);
} 

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 Python Solution

n = int(input().strip())
people = 5
soma = 0

for i in range(n):
    people //= 2
    soma += people
    people *= 3

print(soma)
    
    
    

Other Solutions

  • HackerRank Save the Prisoner! Problem Solution
  • HackerRank Circular Array Rotation Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Beautiful Days at the Movies Solution
Next Post: HackerRank Save the Prisoner! Problem Solution

Related Posts

HackerRank Two Strings Game Problem Solution HackerRank Two Strings Game Problem Solution c
HackerRank Minimum MST Graph Problem Solution HackerRank Minimum MST Graph Solution c
HackerRank Counting Sort 2 Problem Solution HackerRank Counting Sort 2 Problem Solution c
HackerRank Climbing the Leaderboard Problem Solution HackerRank Climbing the Leaderboard Solution c
HackerRank Strange Counter Problem Solution HackerRank Strange Counter Problem Solution c
HackerRank Beautiful Quadruples Problem Solution HackerRank Beautiful Quadruples Problem Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme