Hackerrank's Problem Solving solutions( Time Conversion, Grading Students, Apple and Orange ) :

Problem 10: Time Conversion

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()
{
string s,r;
cin>>s;
if(s[8]=='A') //Time in AM
{
if((s[0] - '0')==1 && (s[1] - '0')==2) //12 AM means hour = 00
{
r=s.substr(2,6); //this substring r contains minute and second
cout<<"00"<<r<<endl;
}
else // if not 12 AM everything as it is printed
{
r=s.substr(0,8);
cout<<r<<endl;
}
}
else //time in PM
{
if((s[0] - '0')==1 && (s[1] - '0')==2) //if 12 PM means no change in hours
{
r=s.substr(2,6); //this substring r contains minute and second
cout<<"12"<<r<<endl;
}
else //if not 12 PM therefore add 12
{
r=s.substr(2,6);
cout<<(((s[0] - '0')*10+10)+(s[1] - '0')+2)<<r; //see note below
}
}
}

s[0] - '0' represents the tens of hour and s[1] - '0' represents the ones of hour , so hour value can be represented as (s[0] - '0')*10 + (s[1] - '0') . For example: 25 = 2*10 + 5 . To add 12 so that we get the correct format of time  we added 10 in its tens value and two in its ones value. 

To understand what's this --> string_name[index] - '0' , refer  problem 37  i.e  seven segment display explanation.

 

 

 

 

Problem 11: Grading Students

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 g[100];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>g[i];
}
for(int j=0;j<n;j++)
{
if(g[j]>=38 && g[j]%5==3)
{
g[j]=g[j]+2;
}
else if(g[j]>=38 && g[j]%5==4)
{
g[j]=g[j]+1;
}
else
{
g[j]=g[j];
}
}
for(int p=0;p<n;p++)
{
cout<<g[p]<<endl;
}
}

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

 

 

 

Problem 12: Apple and Orange

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 s,t,a,b,m,n;
long long ap[100000],apn[100000],orr[100000],orn[100000];
cin>>s>>t>>a>>b>>m>>n;
for(int i=0;i<m;i++)
{
cin>>ap[i];
}
for(int j=0;j<n;j++)
{
cin>>orr[j];
}
for(int k=0;k<m;k++)
{
apn[k]=a+ap[k];
}
for(int l=0;l<n;l++)
{
orn[l]=b+orr[l];
}
long long apple=0;
for(int x=0;x<m;x++)
{
if(apn[x]>=s && apn[x]<=t)
{
apple++;
}
}
long long orange=0;
for(int y=0;y<n;y++)
{
if(orn[y]>=s && orn[y]<=t)
{
orange++;
}
}
cout<<apple<<endl;
cout<<orange<<endl;
}

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

 

 

 

Guys , if you have any queries related to a question or need more explanation for a question , 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 ) :