HackerEarth's basic programming solutions( Divisible, Seven-Segment Display, A. Movement ) :
Problem 36: Divisible
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)
To understand the solution, we must know the divisibility rule for 11 i.e we have to consider the digits at even places as well as odd places of the final number formed. If we put the rule in simple way, we can say that given a number, if we add all the digits at even places and then subtract the summation of digits at odd places , the resultant must be divisible by 11 or the result is 0.
For example: consider the number : 188452
sum of digits at even places = 1+8+5 = 14
sum of digits at even places = 8+4+2 = 14
Now, (14 - 14) = 0 which means 188452 is divisible by 11.
What i have done in the question is, consider a variable sum which is initially equal to zero. If the digit is at even place we will add it to sum and update it's value. If the digit is at odd place we will subtract it from the sum and update it's value. Doing this will give us the desired result. Now with the help of sum value we can find out whether the number formed is divisible by 11 or not.
Problem 37: Seven-Segment Display
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)
Whenever there is a digit in a string , we cant directly do this string_name[index] = = 2 as string is made up of characters and so this is not valid. What's valid is this string_name[index] - '0' = = 2.
In case , the total no. of matchsticks are even , we can simply say that the maximum number that can be formed will be 1111... where no. of 1s are equal to (no. of matchsticks)/2 .
If the total number of matchsticks are odd , then we have to include 7 also as it is made up of 3 matchsticks.
for example: No. of matchsticks = 13 , therefore the numbers can be formed are 711111 or 111117 or 111111( not possible as one matchstick left) , therefore the optimal solution for this is to use 7 in front followed by 1s.
Problem 38: A. Movement
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 code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation, comment down below!
need explanation for problem 36
ReplyDeleteDone!!
Delete