Hackerrank – Viral Advertising
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution For each day calculate how many people like the advertisement. Use the formula from problem description. Then sum all people. I created solution in: Scala Java JavaScript Ruby All solutions are also available on my GitHub. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.*; public class ViralAdvertising { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = Integer.parseInt(stdin.nextLine()); int totalLiked = 0; int dayStartPeople = 5; for(int i = 0; i < n; i++) { int dayLiked = dayStartPeople / 2; int received = dayLiked * 3; totalLiked += dayLiked; dayStartPeople = received; } System.out.println(totalLiked); stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
'use strict'; const processData = input => { const n = parseInt(input) let totalLiked = 0; let dayStartPeople = 5; for(let i = 0; i < n; i++) { const dayLiked = parseInt(dayStartPeople / 2); const received = dayLiked * 3; totalLiked += dayLiked; dayStartPeople = received; } console.log(totalLiked); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |