Wednesday, January 4, 2012

You have a dice which has numbers from 1..6. Another dice is provided to you empty; you need to place numbers on this dice so that whenever both dices are used there is an equal probablity of getting a sum between 1.. 12

Solution::
T
otally there are 6*6 possible combinations from two dices. Since we need the sum from 1..12 with equal probability there should be three combinations among these 36 which sum up to each number in 1 to 12 to achieve a equal probability for all sums.

Other Dice Numbers Presently
_ _ _ _ _ _

G
etting 1 we need 0 on the other dice
Getting 12 we need 6 on the other dice

State of Numbers on other dice now
0 6 _ _ _ _

We need to get SUM=1 in three different combinations and only possibility is 1 and 0 on both dices so put two more 0's on second dice and we have three ways to get 1.

State of numbers on other dice now 0 6 0 0 _ _

Now all the sum's from 7 to 12 can be achieved with one of the numbers be 6 and using the other numbers from first dice. Eg:: 7 can be 1 and 6 from dice 1 and dice2 and if we put two more sixes in dice2 then again 1 and other 6's on dice2 give us 3 ways for seven
Similarly for 8 we can 2 from dice1 and the three sixes one by one we will have 3 ways for 8 and so on for all numbers from 7 to 12. Again we can use the three 0's on dice2 in the similar way we have used three sixes above for getting three ways for sum 1 to 6.

State of numbers on other dice now 0 6 0 0 6 6

Friday, December 9, 2011

In how many ways 3 identical coins can be placed in 5x5 grid so that no two coin come in same row and same column

First coin can be placed in one of 25 places. For second, only 16 places are possible because of the same column and same row condition. Similarly, only 9 places for third coin. So, total ways = 25 * 16 * 9. but since coins are identical you can't distinguish one from another and thus final answer is ( 25*16*9 ) / 3 = 1200.

Feel Free to Let Us Know Your Comment if anything wrong ?

Sunday, October 30, 2011

Two_envelopes_problem

  1. Someone gives you two envelopes with cash inside, and tells you that one of the envelopes has twice as much as the other. You're asked to pick an envelope and open it. You pick Envelope 1 and you open it to reveal $100. Awesome! And now that person asks you if you'd like to switch to Envelope 2, for a cost of merely $5. What should you do ?
  2. The basic setup: You are given two indistinguishable envelopes, each of which contains a positive sum of money. One envelope contains twice as much as the other. You may pick one envelope and keep whatever amount it contains. You pick one envelope at random but before you open it you are offered the possibility to take the other envelope instead.[3]
  3. The switching argument: Now suppose you reason as follows:

  1. I denote by A the amount in my selected envelope.
  2. The probability that A is the smaller amount is 1/2, and that it is the larger amount is also 1/2.
  3. The other envelope may contain either 2A or A/2.
  4. If A is the smaller amount the other envelope contains 2A.
  5. If A is the larger amount the other envelope contains A/2.
  6. Thus the other envelope contains 2A with probability 1/2 and A/2 with probability 1/2.
  7. So the expected value of the money in the other envelope is

    {1 \over 2} (2A) + {1 \over 2} ({A \over 2}) = {5 \over 4}A
  8. This is greater than A, so I gain on average by swapping.
  9. After the switch, I can denote that content by B and reason in exactly the same manner as above.
  10. I will conclude that the most rational thing to do is to swap back again.
  11. To be rational, I will thus end up swapping envelopes indefinitely.
  12. As it seems more rational to open just any envelope than to swap indefinitely, we have a contradiction
  1. The puzzle: The puzzle is to find the flaw in the very compelling line of reasoning above

  1. http://en.wikipedia.org/wiki/Two_envelopes_problem

Thursday, October 20, 2011

Whats The Expected Number of Tosses to to Get Head.

Getting a Head a probability of p and getting a tail of probability 1-p .then find Whats The Expected Number of Tosses to to Get Head.?

Thursday, August 25, 2011

what is the expected number of times he will have to toss his coin to see that pattern.

One day Rohil was getting very bored so he was tossing an unbiased coin randomly. He observed that certain patterns (a sequence of Head and Tail) appear very frequently while some other are very rare. Being a programmer he decided to code a solution which takes a pattern string as input and tells what is the expected number of times he will have to toss his coin to see that pattern. He wrote this program very quickly. Can you?

Input

First line contains ( 1<=T<=25 ) the number of test cases. Each of following T lines contains a pattern string of 'H' and 'T' only. H is for Head and T is for Tail.

|S| <= 40
Output

For each test case print the output in a new line (it is guaranteed that answer will always be an integer and fits in 64 bit type).
Example

Input:
3
H
HTHT
TTHTHTHTHTHHTHTHTHTTTTTTHTHHHHHTT

Output:
2
20
8589934598

Solution http://www.se.cuhk.edu.hk/~seem3570/12-pattern.pdf
http://deepblue.lib.umich.edu/bitstream/2027.42/24435/1/0000708.pdf

Thursday, August 11, 2011

6 Bullet & a Pesron Puzzle

You have been taken hostage by terrorists. Terrorist has a revolver with 6 empty chambers. He now loads the revolver with 2 bullets in front of you. These two bullets are kept next to each other (contiguously) in the 2 adjacent chambers, the terrorist now spins the barrel so that now you don't know what is location of bullets in chamber. He keep the revolver against your head and pulls the trigger. But its lucky day for you. It was the empty chamber. Now terrorist gives a slight chance to get free. He gives you a choice whether he should spin the barrel first then pull the trigger again.. or should just simply pull the trigger again ? If you get lucky again, terrorists will let you go..otherwise ..!!!


So what would you prefer ? Would you want him to spin the barrel first then shoot or just shoot without spinning the barrel ?

Minimum number of cuts to the bar of gold that will allow you to pay to worker 1/7th each day ?

A worker is to perform work for you for seven straight days. In return for his work, you will pay him 1/7th of a bar of gold per day. The worker requires a daily payment of 1/7th of the bar of gold. What and where are the fewest number of cuts to the bar of gold that will allow you to pay him 1/7th each day?

Solution
Gold bar need minimum two cuts. It should be divided in powers of two.
e.g. for i=0 to k where 2^k is nearly equals to N length of gold piece. 1 ,2 , 4........assuming 7 parts which is divided into a total of 1,2,4.

first day : 1 ---(1)
second day: take back one and give 2----(2)
third day : give back one----(1,2)
fourth day : take back 2,1 and give 4 ----(4)
fifth day : give back 1------(1,4)
sixth day : give 2 and take back 1-----(4,2)
sevent day : give back one-------(1,2,4)