Showing posts with label puzzle. Show all posts
Showing posts with label puzzle. Show all posts

Saturday, September 26, 2009

One of the greatest chess puzzle

Lately, I have grown a bit of interest in chess, thanks to Ramprasad. He pointed to this page. Truely, an amazing puzzle indeed. Holy Grail!!!!

Just copying the positions in FEN here for those out there who are curious to solve it first :-).

Bq1B1K2/3PpN2/P3Pp2/P1p2P2/2Pk1b1R/1p6/pN1P1P2/QR6 w - - 0 1

White to move and mate in 4.

Wednesday, January 16, 2008

Logic and Games

Hi All,
Well, it has been some time since I have posted anything technical on this blog. Lately I have been very lazy about writing down interesting things over here. The biggest pain that I face is that there is no support for latex in blogspot.

Anyways, coming down to the point. I am attending a few lectures by Rohit Parikh at Logic School. He is a very good orator, I must say. As a part of his talk on "Finite and Infinite Dialogues", he mentioned a very famous product-sum puzzle. I am writing it down here just for the sake of completeness.

----

Mr P and Mr S are told product and sum of two number a and b respectively, where 2<= a,b <= 99.
P : I don't know the numbers.
S : I knew you did not know them.
P : Now, I know the numbers
S : Now, I know them too.

the solution can be found from dijkstra's paper.
----

With this example, Prof. Rohit went on to describe interesting world of finite and infinite dialogs between two parties in order to determine certain information. Yet another interesting example that he gave was as follows :

A randomly chosen positive integer n is chosen. One of them is written on Ann's forehead and n+1 is written on Bob's forehead. Now, each of them are asked to determine the numbers written on their forehead.

If 1 is written on Ann ( called as A now on ) and 2 is written on Bob ( called as B from now on ). Bob can immediately deduce that his number is 2. If A has got 3 and Bob has got 2 then A have two possibilities 1 or 3. When Bob says I don't know, A can determine that number she has got is 3. It is easy to prove by induction how they can determine their numbers in linear time with respect to number n.

One can devise a game where it would take infinite amount of time to determine one's number. Here is the interesting lecture notes which covered these beautiful examples.

PS : Please note that lecture notes/papers may be copyrighted. Contact the authors in case one wish to use it for non-academic purpose.

Wednesday, January 02, 2008

Water Jug Puzzle

Hi All,
So I am back with a classic water jug puzzle - Courtesy Vaibhav Gupta.
--- Problem ---
I have 3 jars of capacity 19 L, 13 L and 7 L.
13 L and 7 L are completely filled with water.

Give me two jars of 10 L of water.

You are given only 20L water so no spilling is allowed.

--- Solution 1 by vaibhav ---
19L, 13L, 7L (In same order)
Initial conf ==> 0 , 13, 7

0, 13, 7
13, 0, 7
19, 0 , 1
19, 1, 0
12, 1, 7
12, 8, 0
5, 8, 7
5, 13, 2
18, 0, 2
18, 2, 0
11, 2, 7
11, 9, 0
4, 9, 7
4, 13, 3
17, 0, 3
17, 3, 0
10, 3, 7
10, 10, 0
Done. :D

-- Solution 2 by achyut ---

Tilt 13L jar and pour water into 19L upto the point where water surface coincides with the diagonal of the jar, thus exactly pouring half of the water ( 6.5L ) into 19L jar.

Do the same with 7L jar thus we will have 10L ( 6.5L + 3.5L ) in 19L jar.

Pour the remaining 3.5L from 7L jar into 13L jar again making 10L ( 6.5L + 3.5L ).

Now that is called out of the box thinking !!!
------------

Sunday, August 26, 2007

Game of Coins

So, I am back again with an interesting puzzle. Credit goes to my dear friend Sambuddha Roy to introduce me to this puzzle as well as its very simple solution.

Here goes the puzzle :

Even number of coins are laid out in a straight line. Coins may have different denominations. Two players p1 and p2 are playing a game. When a player's turn comes he/she has to pick a coin from either end of the line. When all the coins are exhausted, player with the highest amount wins. A tie occurs whent both the player possess the equal amount.

Prove that if p1 goes first, he/she always have the stretagy such that he/she does not loose the game.


Solution :

Solution to this puzzle is very simple and I really felt really stupid when I came to know about the solution and its simplicity. As always, I tried to use induction to prove that p1 always have a winning stretagy ( or non-loosing stretagy). For the case when number of coins is 2, p1 can pick the larger of the two. But then things get complicated when we need to prove the induction step. In fact, I did not spend enough time to prove that using induction.

A very simple solution is that p1 can index the coins from 1 to n. Then, he can calculate sum total of odd-numbered coins as well as even-numbered coins. p1 can always pick coins in a way such that he/she always gets even-numbered or odd-numbered coins depending on which of those will make up a larger amount.

Monday, June 25, 2007

Lion's Hunger

Here goes yet another puzzle, not as good as the one with the thieves.


Problem
------------

In a zoo far far away ( I am being a little dramatic, eh? :D ), there was a cage filled with n lions. One day the care taker of the zoo put in only one huge meat loaf in the cage. If any lion eats the meat, he will feel dizzy and will fall asleep. Now, that the lion has fallen asleep, he would serve as meat for other lions. Also, assume hypothetically that all lions are intelligent, so none of them would want to get killed.

If you are one of the lion and given an option to eat the meat, would you go for it??


Solution
-----------

Like many of the interesting problem in mathematics, this can be solved using the principle of mathematical induction.

If you are the only lion in the cage, you can go ahead and have your food.

If two lions are there, none of them would eat. why?? Because, if any one of them eat the meat then he is going to be killed by the other lion.

What if three lions are there? you know that if you eat the meat, you will fall asleep. Though you will be as good as meat for other two lions during your slumber, none of them would eat you for the reason stated above.

It is easy to generalize and see that you can have your meal if odd number of lions (including yourself ) are in the cage. For the case of even number of lions, it is better to stay hungry :p.

Tuesday, June 19, 2007

Honour among thieves???

Hi All,
Seems I have been too frequent in posting on my blog now a days. Thanks to my friends Sanjeet, Pranjal, Ashok ... with whom I discuss interesting problems. Just like my previous post, this post is also regarding a technical interview question asked in some company. Anyways, coming directly to the point.

Problem
-----------
There are five thieves T1, T2, T3, T4 and T5. They robbed 100 gold bars from a bank. For dividing the gold bars they came up with following idea.

- Starting from T5 to T1, a thief T_i will come up with a scheme to divide the gold bars amongst the thieves.
- Voting will happen in favour or against this scheme. If Majority ( Strictly greater than 50 % ) votes in favour then the division will happen according to the scheme. If not, then T_i will be killed and it will be T_i-1's turn to come up with a scheme.

Please keep in mind that
1) All thieves are intelligent.
2) All are very greedy, so they would try to get as much wealth as possible.
3) They all would like to kill as many thieves as possible. Of course, this motive stands at a lower priority level as compared to their greed for the gold.

If you are T5, what scheme would you come up with? Obviously, your motive would be to survive but try to come up with a scheme where you will get away with as much gold as possible.
PS:- Smallest unit of division is a gold bar. A bar can not be further divided.

-----
Solution
----
The answer is that if you are T5, you can get away with as much as 97 gold bars and still survive!!! Puzzled!! Read further to discover how.

Let's try to situation one by one from thieves perspective.

If T1 is the only person left, he will take all the gold bars.

If T1 and T2 are the only persons alive, no matter what scheme T2 comes up with, he is going to be killed and T1 would take everything. Because T2 can not get the majority ( > 50% ). Obviously, T2 would do anything to keep T3 alive.

T3 knows that T2 is going to support him no matter what scheme he comes up with. T3 can take away all 100 gold bars as he will have the majority no matter what. ( T3+T2 = 66% votes ). Obviously, T3 would like T4 to get killed so that he can take everything.

T4 knows that T3 would want him to be killed. If T4 is killed, T1 and T2 would not get anything. So T4 would give one gold bar to T1 and T2 each. Now, T4 will get the majority ( T1 + T2 + T4 = 75%) and he can take as much as 98 gold bars.

You are T5. If you get killed, you know that T3 would not get anything. So to take T3 into confidence you will give him a gold bar. If you get killed, T1 and T2 each get a gold bar. As you want to optimize the amount that you can get away with, you would give one of T1 or T2 2-gold bars. You can get away with as much as 97 gold bars!!!

Monday, June 18, 2007

Young Tableau

Hi All,
The other day me, sanjeet, and pranjal were discussing a few questions ( or puzzles ) which are likely to be asked in technical interviews. One of the question that was asked to someone in google was about finding out a specific element in an n x n Young Tableau.

Let me first define, what a young tableau is. It is an m x n array with the following property.

for all j = 1 to n-1, A[p][i] <= A[p][i+1]
and for all , i = 1 to m-1 , A[i][q] <= A[i+1][q] It is easy to see that following statement holds for a young tableau.
  1. For any sub-matrix of a young tableau, the top-left element is the smallest element within that sub-matrix.
  2. For any sub-matrix of a young tableau, the bottom-right element is the largest element within that sub-matrix.
Now, the problem is to find out a given element within an n x n young tableau.

----
Method -1
----

Start with the top-right element of the tableau. Compare this element e with the element to be found x.
if e > x then
discard the column in which e appears. ( Because e is the first(smallest ) element of that column.
else
discard the row in which e appears.
end if.
Repeat this exercise until the element is found or the tableau is exhausted.

It is easy to see that time complexity of Method-1 is O(n). As at every step we will discard either row or column which will take at most 2n steps.

---------
Method - 2
---------
Do a binary search over the diagonal of the tableau. If we find the desired element x then we are done. If not, we will have two element u and v where u <> u is the bottom-right element and v is the top-left element. The elements that we will discard at each step would be at least half of the elements in the original matrix.
Here is why,


i^2 + (n-i)^2
function is minimum where i=n/2. With i=n/2 the value of the function is (n^2)/2.

Now, if we do the analysis with respect to number of elements.
T(n^2) = 2T((n^2)/4) + logn

or

T(m) = 2T(m/4) + 0.5 logm

Solution of this recurrence, turns out to be O(m^(0.5)) = O(n). So, there is no improvement over the complexity as compared to the first method. Besides, this second method fails when the sub-matrices that were discarded were not square. In which case, the remaining two sub-matrices will not be square either.
----
Method 3
------
Compare the central element
e with the desired element x.
if e > x then
discard the bottom-right quadrant of the tableau.
else
discard the top-left quadrant of the tableau.

Now , we are left with three sub-matrices of the size n/2 x n/2.

The recurrence would be

T(n) = 3T(n/2) + O(1).

Solving it would give us n^(log_2 3) = n^1.585. It turns out that even though in Method-1 you discard n elements and in this method you discard (n^2)/4, the complexity does not favour Method-3. Anyways, it was a good exercise to do this analysis.

Thursday, June 14, 2007

Necklace puzzle - yet another solution

Hi all,

I am posting here another solution to "Necklace Puzzle". The solution is by my friend Vaibhav Gupta

-------


Let A and B are the 2 necklaces with N gemstones. A and B has three types of
gemstones which are equal in number.

A = A[1] A[2] ... A[N]
B = B[1] B[2] ... B[N]

Where A[i] is the ith gemstone in Necklace A.
B[i] is the ith gemstone in Necklace B.

Let Color(A[i]) represents the type of the gemstone A[i].

We construct a bipartite graph G = (X,Y,E), with the following condition:

For each gemstone A[j] in A we create a node X_j in X. So |X| = N
Foe each gemstone B[j] in B we create a node Y_j in Y. So |Y| = N
E is the edges set from node in X to node in Y.

E = { (u,v) | u \in X and v \in Y and Color(u) = Color(v)}

|E| = (N^2) / 3 since each node has exactly N/3 edges.

We define weight to each edge in E:
Weight(e) = orientation of the string.
OR Mathematically
Weight((X_i,Y_j)) = i - j if i>=j
= N+i-j otherwise

0 <= Weight((X_i,Y_j)) <= N-1 since 1<=i<=N, 1<=j<=N So there are N possible values of weight.

Now we just need to prove that there are atleast N/3 edges of same weight.
This can be proved by pigon hole principle. There are (N^2) / 3 edges and
each edge has N possible values of weight so there must exist a set of
ceil(|E|/N) edges of same weight

|E| / N >= N/3

Hence proved.

For general case :

For 3 gemstone types
( a^2 + b^2 + c^2 ) / (a + b + c)

Where a is the number of gemstone of type 1.
b is the number of gemstone of type 2.
c is the number of gemstone of type 3.

For m gemstone types:
(N_1^2 + N_2^2 + N_3^2 ... + N_m^2) / (N_1+N_2+N_3 ..+N_m)

where N_i is the number of gemstone of type i.

---------

Wednesday, June 13, 2007

Necklace Theorem - Solutions to puzzles

Hi All,
So I am back again after some gap. This post will describe the solutions to the two puzzle that I posed as part of my prequel post "Necklace Theorem". For the sake of clarity I am copying the puzzles again over here.

1) Given a point set of size n in 2D, prove that there would exist a pair of orthogonal lines which would divide the point set in such a way that in each quadrant number of points are at most n/4.
As shown in the figure on the left, let us consider a baseline B. The circle represent the point set we are interested in. Refer to the prequel of this post and convince yourself that it is possible to have a line in any given direction which divides the point set into two equal half. Hence, it is also possible to have two orthogonal lines both of which will divide the point set into two equal half. ( When I say, two equal half, I mean that division in two portion satisfying the constraint "less or equals n/2" ). As both p and q divides the point set into two equal half, the region which is captured on top-left and bottom-right will be equal ( call it x, or to be more precise "less or equal x" ). Similarly, tom-right and bottom-left would be equal ( call it y). Let us have a function f(x,y) = (x-y) = v. By rotating p and q , 90 degrees we will have function value exactly negative, as x and y would swap their places. From intermediate value theorem we can argue that there will be a configuration where x = y = n/4.

2) Given a point set of size n in 2D, prove that there would exist 3 concurrent lines which would divide the point set in such a way that in each segment the number of points are at most n/6.

With respect to the baseline B, we can have two lines p and q, each dividing the point set into equal half and region trapped as shown in figure would be equal to n/6 on each side. ( n/6 is written outside the circle to make it readable ). Now, as shown in the prequel post, we can have a line r which will divide the two point sets ( each of size n/3 ) into two equal half. This line r need not be concurrent with p and q. Let the triangle trapped between these three lines is equal to A. As we rotate this configuration 180 degree the area will become -A. Again, from intermediate value theorem we can state that area will be 0 at some configuration making r concurrent to p and q. Thus, each segment will have size n/6.

Tuesday, May 29, 2007

Necklace Puzzle

Hi All,
So I am here back again with a new interesting puzzle ( courtesy Vaibhav Gupta ). I am copying his mail verbatim below followed by the solution that I have in mind.
---------- Puzzle ------
NECKLACE PUZZLE #2

There are two circular necklaces with same number of three types of gemstones,
but the gemstones may be strung in different order along the necklace.

Now the two necklaces are placed on one top of the other so that the
gems are aligned
one above the other. Count the number of locations where the pair of
gems (one from
the lower necklace, one from the upper) is of the same type.
Since one necklace can be rotated relatively to the other, there are
many orientations
in which the count of matching gems can be taken.

Prove that there is at least one orientation where the number of
matches is at least
a certain number v.

For the special case of equal numbers of each kind of gem, v = N/3,
where N = total number of gems in one necklace (and 3 kinds of gems).

1. prove this for the special case.
2. find, with proof, value of v in the general case (with m kinds of
gems not necessarily equal in number).

some clarifications:
1. the necklace is circular i.e. its a simple closed loop. so given
two necklaces RGB and RBG,
the second can be rotated wrt first to give three orientations (but
this small example
is unusual because the number of matches is same in all 3 orientations):

RGB
RBG 1 matches

RGB
GRB 1 matches

RGB
BGR 1 matches

so v = 1

2. "there is at least one orientation where the number of matches is
at least v"
label the orientations from 1 to N. denote the number of matches for
orientation i as m[i].
then " there exists an i such that m[i] >= v" is the more formal way
of saying this.


-Vaibhav.
------------------
Well, indeed an interesting puzzle. Intially for a few minutes I thought whether it can be proved by contradiction or not. I tried to see whether I am able to hit a contradiction by imagining that "v is always less than N/3". Within a few minutes I realized that this is not going to work. As I remembered the lessons from Prof Sundar Vishvanathan , I thought of trying to prove it by induction. Prof Sundar always used to say that most of the proofs can be given by the principle of mathematical induction. This time I did not bank too much on this options as I learnt it from my earlier experience as I mentioned in my previous post. The solution of this proof also along the line which was taught to me by Prof Sundar.

The proof here makes use of the fact that if expected value of some random variable is a then there exist a possibility where the random variables has a values at least a.

Let us assume that first necklace is fixed and the second necklace is rotated with respect to the first one. Let us define a random variable X such that
X_i = 1 ( if ith gem of first necklace matches with the second one )
X_i = 0 Otherwise.

The ordering in the second necklace can be anything so assuming a uniform distribution there.
Calculating the expected value,
E(X_i) = Pr(X_i).X_i = (Number of gems having the same colour as ith gem/Total number of gems)
For 3 different kind of gems
E(X) = Sum(i=1 to N) ( E(X_i) ) = R^2/N + B^2 /N + G^2/N.

For the special case where R = B = G = N/3 we will have
E(X) = N/3. As the expected value is N/3 we can say that there is at least one configuration of second necklace which would make the number of matching gems at least N/3.

When there are m kinds of gems, not necessarily equal in number, let C[i] denote the number of gems of the same kind as i, where i varies from 1 to m.
Therefore, for general case the expected value is
E(X) = Sum (i = 1 to n ) ( C[i]^2/N )

Well, a nice puzzle indeed. As a closing remark, I have the solutions to the puzzle that I mentioned in my previous post. It will require some figures to be embedded to explain the solution in a lucid way. I will post it soon as I get the time.