Viral Advertising - HackerRank || HackerRank Viral Advertising Solution
Problem Name: Viral Advertising
Judge: HackerRank
Problem link: Viral Advertising
HackerRank Viral Advertising Solution in C
#include <stdio.h>
int main()
{
int d, shared = 5, liked = 2, cumulative = 2;
scanf("%d", &d);
for (int i = 2; i <= d; i++)
{
shared = (shared / 2) * 3;
liked = shared / 2;
cumulative = cumulative + liked;
}
printf("%d\n", cumulative);
return 0;
}
HackerRank Viral Advertising Solution in C++/Cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int d, shared = 5, liked = 2, cumulative = 2;
cin >> d;
for (int i = 2; i <= d; i++)
{
shared = (shared / 2) * 3;
liked = shared / 2;
cumulative = cumulative + liked;
}
cout << cumulative << endl;
return 0;
}
HackerRank Viral Advertising Solution in Java
import java.util.Scanner;
public class Viral_Advertising {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int d = input.nextInt();
int shared = 5, liked = 2, cumulative = 2;
for (int i = 2; i <= d; i++) {
shared = (shared / 2) * 3;
liked = shared / 2;
cumulative = cumulative + liked;
}
System.out.println(cumulative);
}
}
