HackerEarth's basic programming solutions( Ali and Helping innocent people, Duration, Book of Potion making ) :
Problem 21: Ali and Helping innocent people
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 count;
string s;
cin>>s;
if((((s[0]-'0')+(s[1]-'0'))%2==0)&&(((s[3]-'0')+(s[4]-'0'))%2==0)&&(((s[4]-'0')+
(s[5]-'0'))%2==0)&&(((s[7]-'0')+(s[8]-'0'))%2==0)&&(s[2]!='A') &&
(s[2]!='E') && (s[2]!='I') && (s[2]!='O') && (s[2]!='U') && (s[2]!='Y') )
{
cout<<"valid"<<endl;
}
else
{
cout<<"invalid"<<endl;
}
}
Since the string structure is same in each output , we can simply write all the valid conditions in the if parenthesis.
AND is used , therefore all conditions must be true in order to print valid.
Problem 22: Duration
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 t,p,q,r,s,md,hd;
cin>>t;
while(t--)
{
cin>>p>>q>>r>>s; //p=SH , q=SM , r=EH , s=EM
if(q>s)
{
q=60-q;
md=q+s;
hd=r-(p+1);
cout<<hd<<" "<<md<<endl;
}
else if(q==s)
{
md=0;
hd=abs(p-r);
cout<<hd<<" "<<md<<endl;
}
else{
md=s-q;
hd=(r-p);
cout<<hd<<" "<<md<<endl;
}
}
}
You can use the example to understand the code.
input output
2 0 30 1 44 2 14 5 41 2 42 8 23
Problem 23: Book of Potion making
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 sum;
string s;
cin>>s;
sum=(s[0]-'0')*1+(s[1]-'0')*2+(s[2]-'0')*3+(s[3]-'0')*4+(s[4]-'0')*5+(s[5]-'0')*6
+(s[6]-'0')*7+(s[7]-'0')*8+(s[8]-'0')*9+(s[9]-'0')*10;
if(sum%11==0)
{
cout<<"Legal ISBN"<<endl;
}
else
{
cout<<"Illegal ISBN"<<endl;
}
}
Since the string structure is same in each output , we can simply write all the sum and then check whether it's divisible by 11 or not.
Guys , if you have any queries related to a question or need more explanation, comment down below!
Comments
Post a Comment