Hackerrank's Problem Solving solutions( Picking Numbers, The Hurdle Race, Designer PDF Viewer ) :

Problem 25: Picking Numbers

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,max=0,c,d;
cin>>n;
vector<int>v(n);
for(i=0;i<n;i++)
{
cin>>v[i];
}
for(i=0;i<n;i++)
{
c=count(v.begin(),v.end(),v[i]);
d=count(v.begin(),v.end(),v[i]-1);
c=c+d;
if(c>max) //to find out the maximum value of c
{
max=c;
}
}
cout<<max;
}

In this question we have to find the length of the subarray from the given array which includes elements such that absolute difference between any two elements is less or equal to 1 i.e abs(x-y)<=1

What i have done here is we will count the occurrence of every element of the vector represented by c and then all the occurrences of the (element-1) represented by d so that we will cover only the elements whose absolute difference is less than or equal to 1

For example : v = { 1 , 1 , 2 , 2 , 4 , 4 , 5 , 5 , 5 }

The first iteration of the for loop will consider the first element of the vector ,

Therefore , c = 2 ( the number of occurrences of 1)

                   d = 0 ( the number of occurrences of 0 i.e 1-1)

now c = c+d ==> c = 2 ;

This process makes sure that we are considering only those elements whose absolute difference is less than or equal to 1.

Similarly this is done with each element of the array or vector till we get the maximum value of c ( of course it will be the length of the subarray or i say number of elements from array which are needed to make the subarray )

 

 

 

 

 

 

Problem 26: The Hurdle Race

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,k,p=0,h; //p=no. of potions , h=height of the hurdle
cin>>n>>k;
while(n--)
{
cin>>h;
if(h>k)
{
p=p+(h-k); //no. of potions needed to cross the hurdle
k=h; //updating his maximum jump
}
}
cout<<p;
}

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

 

 

 

 

 

 

Problem 27: Designer PDF Viewer

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 max=0,i,j=0;
vector<int>v(26);
for(i=0;i<26;i++)
{
cin>>v[i];
}
map<char,int>m; //you will understand this once you have done map STL
for(char p='a';p<='z';p++)
{
m[p]=v[j];
j++;
}
string s;
cin>>s;
for(i=0;i<s.length();i++)
{
if(max<m[s[i]])
{
max=m[s[i]];
}
}
cout<<max*s.length();
}

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

 

 

 

 

 

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