2021 Updated Viral Advertising HackerRank Solution in C++

 

        Coding Demons has solved "Viral Advertising" Hackerrank problem for you. Hacker Land Enterprise  adapts a new viral advertising method. Whenever a new product is launched by them in the market, they advertise it only to specific five people on social media. Half of the 5 people like the advertisement and they share it. (floor(5/2)= 2). Each person shares it with 3 other people.

        On the beginning of second day 6 people have received the advertisement. [floor(5/2) *3=2*3=6]. Everyday the same cycle repeats as, floor(recipient/2) likes the advertisement and each one of them share with 3 friends. Assume nobody sees the advertisement twice. You need to find out how many people have liked the advertisement by the end of the given day.


        Find the total number of people who likes the advertisement.

Example :

 Input : 3

Output : 9


Day 1 : Shared to 5 people
             Liked by 2 [floor(5/2)=2]      
             Total 2 people liked advertisement

Day 2 : Shared to 6 people [liked by 2 on day one and each shared to 3 friends (2*3=6) ]
             Liked by 3 [floor(6/2)=3] 
             Total 5 people liked advertisement [2 from day one & 3 from day two (2+3=5) ]

Day 3 : Shared to 9 people [liked by 3 on day two and each shared to 3 friends (3*3=9) ]
             Liked by 4 [floor(9/2)=4] 
             Total 9 people liked advertisement [5 from day two & 4 from day three (5+4=9) ]
 
Hence, the Total Number Of People who liked advertisement is 9.


Read : How to approach Coding as a Beginner in 2021 


Logic: 

  • Take starting day, ending day, and divisor.
  • Make a function to find the reverse of a number.
  • Set counter to store no of beautiful days.
  • Subtract the reverse from the original number.
  • Check whether it is evenly divisible by k or not.
  • If the number is evenly divisible, increase the counter of beautiful days.
  • Print the counter.


Code :


#include <bits/stdc++.h>
using namespace std;

int main()
{
    
int no_of_days, count;
cin>>no_of_days;

int share = 5;

for(int i=0;i<no_of_days;i++)
{
    int half = share/2;
    count = count + half;
    share = half*3;
}
cout<<count<<endl;
    return 0;
}

Post a Comment

0 Comments