Viral Advertising

Viral Advertising

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., ) 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 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.

For example, assume you want to know how many have liked the ad by the end of the 5th day.

1
2
3
4
5
6
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 cumulative number of likes is 24.

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, n, denoting a number of days.

Constraints

  • 1 <= n <= 50

Output Format

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

Sample Input

1
3

Sample Output

1
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.


Solution

1
2
3
4
5
6
7
8
9
10
11
// Complete the viralAdvertising function below.
function viralAdvertising(n) {
let { total } = new Array(n).fill(0).reduce((target, day) => {
target['total'] += Math.floor(target['people'] / 2);
target['people'] = Math.floor(target['people'] / 2) * 3;

return target;
}, {total: 0, people: 5})

return total;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×