Hackerrank's Problem Solving solutions( Viral Advertising, Save the Prisoner!, Circular Array Rotation ) :

Problem 31: Viral Advertising

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,i,r=5,likes,ct=0; //ct=count , r=initial no. of people
cin>>n;
for(i=1;i<=n;i++)
{
likes=floor(r/2); //as said in the question
ct=ct+likes;
r=likes*3; //updating r
}
cout<<ct;
}

This code is simple. There's no need for any explanation.

 

 

 

 

 

 

 

Problem 32: Save the Prisoner!

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n,m,s;
cin>>t;
while(t--)
{
cin>>n>>m>>s;
cout<<(((s-1+m-1)%n)+1)<<endl;
}
}

"From where the hell this formula came" , after reading the solution your mind will probably say that. Let me explain : here n = no. of prisoners , s = seat number from which we will start distributing , m = no. of  candies. 

Suppose we always start distributing from the first prisoner then in that case we will have use modulo if m is greater than the number of prisoners and since modulo is best when period starts from zero therefore let the first prisoner be at zeroth seat. The idea of modulo and s-1 came from here.

The m-1 handles the fact that the sweet which will be given to first prisoner( according to s value) is not counted when giving away sweets. for example suppose s = 8 and number of sweets are 2 therefore (8+m-1) ==> (8+2-1) ==> 9 i.e 9th seat member should be warned.

The +1 in the end is to compensate s-1 value since first seat no. is 1 and not 0.

 

 

 

 

 

 

 

Problem 33: Circular Array Rotation

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,k,q,i,m,mo; //see explanation below first
cin>>n>>k>>q;
vector<long long>v(n);
for(i=0;i<n;i++)
{
cin>>v[i];
}
mo=k%n;
vector<long long>v2(n);
for(i=0;i<n;i++)
{
v2[mo]=v[i];
mo++;
if(mo==n) //we reached at the end
{
mo=0;
}
}
while(q--)
{
cin>>m;
cout<<v2[m]<<endl;
}
}

This question is simple and uses the fact that after k rotations the zeroth element will come at index k ( when k is less than n ) or k%n (when k is greater than n) and of course we can use k%n even when k is less than n so therefore , we can say that zeroth element will come at index k%n after k rotations.

For example -  a = [3,4,5] and k = 2 , therefore final array = [4,5,3]  (3 came at index 2 or 2%3=2)

                        a = [3,4,5] and k = 4 , therefore final array = [5,3,4]   (3 came at index 1 or 4%3=1)

Now we can set the elements lying ahead of it easily and we know if we reaches at the end we will have to go to the beginning to put the elements. That's what is done in the solution. We have placed the zeroth element of  vector v at k%n index of the vector v2 and then placing all the elements in order till we reach the end of the vector v2 and if we encounter end (mo = = n) means now we have to put the elements from zeroth index keeping the order.


 

 

 

Guys , if you have any queries or need more explanation for something , comment below! 

 

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :