Hackerrank's Problem Solving solutions( Bill Division, Sales by Match, Drawing Book ) :

Problem 19: Bill Division

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,i,b,sum=0,unfair,fair,refund;
cin>>n>>k;
long long bill[n];
for(i=0;i<n;i++)
{
cin>>bill[i];
sum=sum+bill[i];
}
cin>>b;
unfair=sum/2;
fair=(sum-bill[k])/2;
refund=unfair-fair;
if(b==fair)
{
cout<<"Bon Appetit";
}
else
{
cout<<refund;
}
}

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

 

 

 

 

Problem 20: Sales by Match

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()
{
int n,i,ct=0; //ct=count
cin>>n;
int a[n];
map<int,int>m; //declaring a map STL
for(i=0;i<n;i++)
{
cin>>a[i];
m[a[i]]++; //you will understand this once you have done map STL
}
for(auto &e:m) //map traversal
{
if(e.second%2==0)
{
ct=ct+e.second/2;
}
else
{
ct=ct+(e.second-1)/2;
}
}
cout<<ct;
}

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

 

 

 

 

 

 

 

Problem 21: Drawing Book

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 int n,i,count=0,p,count2=0;
cin>>n;
cin>>p;
if(n%2==0) //-------------------1st if starts------------------
{
if(abs(p-1)<=abs(p-n)) //-----2nd if starts----
{
if(p==1)
{
cout<<"0"<<endl;
}
else
{
for(i=2;i<=p;i+=2)
{
count++;
}
cout<<count<<endl;
}
} //-----2nd if ends-------
else //else of 2nd if starts..
{
if(p==n)
{
cout<<"0"<<endl;
}
else
{
for(i=n-1;i>=p;i-=2)
{
count++;
}
cout<<count<<endl;
}
} //else of 2nd if ends..
} //----------------1st if ends--------------------------

else //--------------else of 1st if starts ------------------
{
if(abs(p-1)<abs(p-n)) //-----3rd if starts----
{
if(p==1)
{
cout<<"0"<<endl;
}
else
{
for(i=2;i<=p;i+=2)
{
count2++;
}
cout<<count2<<endl;
}
} //-----3rd if ends----
else //else of 3rd if starts..
{
if(p==n || p==n-1)
{
cout<<"0"<<endl;
}
else
{
for(i=n-2;i>=p;i-=2)
{
count2++;
}
cout<<count2<<endl;
}
} //else of 3rd if ends..
} //--------------else of 1st if ends-------------------
}

This question is easy. First we will check whether the no. of pages in the book is even or odd. The first if statement tests that n=even. Now we want to determine from where to start start searching the page i.e from the front or back (it can be easily done by comparing the distance of page to be searched i.e p from first page and last page ) therefore , we will use (abs(p-1)<=abs(p-n)) which means we will start from the front page. It's else means we will start from the last page. Similarly else of the first statement means the no. of pages are odd and then we will again see from where to start searching i.e from front or back. It took me hours to solve this question but when i saw the discussion section i was really shocked and amazed also as this question was wrapped in only 3 lines with no explanation how. This solution below is famous in the discussion section -

#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,p;
cin>>n>>p;
cout<<min(p/2, n/2-p/2); //min() returns the minimum value from two given values
}

I also don't know how it is working. If you find out the logic , please let me know in the comment section below.





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 ) :