Thursday, July 28, 2011

given a 6x6 matrix with all the elements as either 1 or -1. find the number of ways the elements can b arranged such that 1.the product of all elements of all columns is 1 2.the product of all elements of all rows is 1

Monday, July 25, 2011

There are four dogs each at the corner of a unit square. Each of the dogs starts chasing the dog in the clockwise direction. They all run at the same speed and continuously change their direction accordingly so that they are always heading straight towards the other dog. How long does it take for the dogs to catch each other and where?

Since the dogs are moving symmetrically, the distance saperating them will decrease evenly as they case each other, so the dogs should catch each other in the center of the room. Their path will be curved, but for sake of approximation, lets se it’s a straigt line to the center.

Assuming that the dogs run at 1m/sec, and room is y x y in diamater.

h^2 = (y/2)^2 + (y/2)^2
h^2 = 2(y/2)^2
h = sqrt(2) * y/2
h = y/sqrt(2) meters

thus it would take the dogs approximtly y/sqrt(2) seconds to catch each other. This is a lower bounds, as the dog will not be traveling in a straight line, but along a curve.

Some Other & Good Aproaches are

Each dog starts at the corner and moving symmetrically. So each dogs start moving perpendicular to the adjacent dogs. Lets assume v.

So each one start moving with v towards the next dog.

If we see realtive speed of the dog1 (v1), w.r.t dog 2, it changes perpendicularly. So it’ll not affect the time taken along the direction of the dog1 to dog2 and the speed will be v only always.

So it they have started at corners with the distance of the length of the square(s).

Time = s/v. They’ll meet at the center.

Another Similar Approach is

Let’s the dogs be A, B, C and D where A is chasing B, B is chasing C, C is chasing D and D is chasing A.
All the dogs will eventually meet in the center of the square. Since all the dogs move in symmetry, the only logical answer to the location of their meeting is the center of the square.
At any point in time, dog A is perpendicular to dog B and B perpendicular to C and so on. Dog A moves towards dog B but dog B does not move towards or away from dog A since it is moving perpendicular to dog A. Therefore, the distance that dog A needs to cover to reach dog B is the same as the original distance between them, one unit.

The speed of each of the dog towards the dog it is chasing is given by (1 + cos(t)) where t is the angle on each corner of the square.

Speed of dog = 1 + cos(90) = 1 + 0 = 1
Time needed = Distance/Speed = 1 / 1 = 1 unit.

A Heavan Puzzle

Wednesday, July 20, 2011

Google Puzzle- who keeps the fish?

Facts:*

1: There are 5 villas in 5 different colors
2: In each villa lives a person with a different nationality.
3: These 5 owners drink a certain beverage, smoke a certain brand of cigar
and keep a certain pet.
4: No owner has the same pet, smoke the same brand of cigar or drink the
same drink.


*Hints:*

1: The British lives in a red villa.
2: The Swede keeps dogs as pets
3: The Dane drinks tea
4: The green villa is on the left of the white villa (it also means they are
next door to each other)
5: The green villa owner drinks coffee
6: The person who smokes Pall Mall rears birds
7: The owner of the yellow villa smokes Dunhill
8: The man living in the villa right in the center drinks milk
9: The Norwegian lives in the first villa
10: The man who smokes Blend lives next to the one who keeps cats
11: The man who keeps horses lives next to the man who smokes Dunhill
12: The owner who smokes Blue Master drinks beer
13: The German smokes Prince
14: The Norwegian lives next to the blue villa
15: The man who smokes Blend has a neighbor who drinks water.

*The question is: who keeps the fish?

Monday, July 4, 2011

Minimum Number of Cuts Needed to Cut Rope in get the rope of desired length

the length of the rope is l units.
I can only cut any rope into two halves. for example if the length of the rope is 8 and we need a length of rope 6 we first cut into two halves and we get 4, 4 now we cut any of the half again and we get 4,2,2 now we can merge 4 and 2 and form a rope of length 6. in this example we need a minimum of 2 cuts to get the length of rope 6 from 8

assume that l is always a power of 2 and we need always a even length of rope from it how to find the number of minimum cuts needed to get the new rope?.

1st Solution

k -> rope of desired length.
l -> rope of given length
m = 2;
while(k%m==0 )
m *= 2;
ans :: (log2(l) - log2(m) + 1).
ex.
k = 6,l = 8
so initially m = 2;
after 1st iteration m = 4;
then break;
so min = log2(8) - log2(4) + 1 = 3 -2 + 1 = 2.


2nd Solution
for a number N
first set bit(From Left) is simply integer value of log(N)
last set bit can be calculated as
N = N-(N&(N-1)); and then Log(N)
int i = log(n);
n -= n&(n-1);
int j = log(n);
i-j will be the answer.

Refer here for more detail