HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :
Problem 1: Seating Arrangement
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)
let's assume L = a larger number and S = a smaller number
therefore - L%S = remainder when L is divided by S
and S%L = S
The beauty of this modulo operator is seen when there comes a period. For example the value of angles are repeated after 360 degree. Now let a larger angle which is greater than 360 be A . If we want the corresponding angle of A between 0 to 360 degree than we can use modulo operator like this ( let the corresponding angle of A is a) :
a = A%360
For Example - A = 4781 , therefore a = 4781%360 = 101
Now getting to the question , the cycle repeats after 12th seat . Therefore the period is 12. Now if we use modulo for any seat we will get the seat within 1 to 12 . For example - 69%12 = 9 and we know the 9th seat is Aisle Seat(AS) and the seat in front of it is 5 less than 9 i.e 4. Therefore we can conclude that the 69th seat is Aisle Seat(AS) and the seat in front of it will be 69 - 5 = 64.
In the above code i used else if ladder and solved the problem.
Problem 2: Zoos
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.
Problem 3:Anagrams
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)
A map stores key , value pairs. Map_name[key_name] returns the value corresponding to the key_name and if in square brackets there's a key_name for which we never declared it's corresponding value , in that case this function returns 0.
Let the string a be: cde and string b be: abc
therefore , according to our code map m1 will look like this:
key value
c 1
d 1
e 1
and map m2:
key value
a 1
b 1
c 1
The for loop will run from c='a' to c='z' , first c = a:
in this case abs(m1[c]-m2[c]) ==> abs(m1[a]-m2[a]) ==> abs(0 - 1) ==> 1 that means 1 deletion because 'a' only lies in one of the string and must be deleted. The count variable stores the no. of deletions.
If the value of abs(m1[c]-m2[c]) = 0 means the character exists in both strings , therefore no deletion required and the value of count remains unchanged.
Guys , if you have any queries related to a question or need more explanation, comment down below!
Comments
Post a Comment