HackerEarth's basic programming solutions( VC Pairs, Arithmetic Progression, Database ) :
Problem 18: VC Pairs
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)
I know you are wondering what's this big crap written inside the if parenthesis. Well let me explain.
&& --> is logical AND . In case of AND all the conditions must be TRUE to make the overall expression TRUE.
|| --> is logical OR . In case of OR only one of the condition must be TRUE to make the overall expression TRUE.
The expression inside the if parenthesis can be broken down into 3 parts -
1. (s[i]!='a' && s[i]!='e' && s[i]!='i' && s[i]!='o'&& s[i]!='u') , here each and every condition is checked and this block of code returns TRUE only if all the conditions inside are TRUE.
2. && , this means we have to check the next block also.
3. (s[i+1]=='a'|| s[i+1]=='e'|| s[i+1]=='i'|| s[i+1]=='o'|| s[i+1]=='u') , here checking starts from left to right. If the computer encounters a TRUE expression , the whole block returns TRUE , since we used OR there's no need to check the other remaining conditions.
The if block will get executed only when both the blocks returns TRUE since there's AND between them.
Problem 19: Arithmetic Progression
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)
You will be wondering right now , how this solution strike in my mind. Well , i will say it's all about observation. The sample input and output given are -
input output
-5 7 6 7 -10 -100 20 105 1 -1 1 2 51 23 10 8
Let's take the example 1 i.e a = -5 , b = 7 and c = 6 , now if we see b-a is not equal to c-b and we have to tell the no. of operations to do so that they follow the rules of arithmatic progression.
Now let's d = difference which we want to reduce so that b-a = c-b therefore d = (b-a)-(c-b)
Calculating d in example 1: d = (7+5)-(6-7) = 12+1= 13 and the answer is 7
Calculating d in example 2: d = (-100+10)-(20+100) = -90-120 = 210 and the answer is 105
Calculating d in example 3: d = (-1-1)-(1+1) = -2-2 = -4 and the answer is 2
Therefore , we can say that , the answer relation with d is: answer = abs(d+1)/2
Problem 20: Database
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)
This is really a tough problem. When i opened this problem , the solution was already there from HackerEarth .
Guys , if you have any queries related to a question or need more explanation, comment down below!
Comments
Post a Comment