Saturday, March 19, 2011

Bus Station Puzzle -A DP Approach

At a bus-station, you have time-table for buses arrival and departure. You need to find the minimum number of platforms so that all the buses can be accommodated as per their schedule.

Example: Time table is like below:


Bus


Arrival


Departure

BusA


0900 hrs


0930 hrs

BusB


0915 hrs


1300 hrs

BusC


1030 hrs


1100 hrs

BusD


1045 hrs


1145 hrs


Then the answer must be 3. Otherwise the bus-station will not be able to accommodate all the buses.


Answer:

Let’s take the same example as described above. Now if we apply dynamic programming and calculate the number of buses at station at any time (when a bus comes or leaves). Maximum number in that pool will be nothing but the maximum number of buses at the bus-station at any time, which is same as max number of platforms required.


So first sort all the arrival(A) and departure(D) time in an int array. Please save the corresponding arrival or departure in the array also. Either you can use a particular bit for this purpose or make a structure. After sorting our array will look like this:


0900


0915


1930


1030


1045


1100


1145


1300

A


A


D


A


A


D


D


D


Now modify the array as put 1 where you see A and -1 where you see D. So new array will be like this:


1


1


-1


1


1


-1


-1


-1


And finally make a cumulative array out of this:


1


2


1


2


3


2


1


0


Your solution will be the maximum value in this array. Here it is 3.

The complexity of this solution depends on the complexity of sorting.

Soln Provided By Aakash

Wednesday, March 16, 2011

Temple,Flowers and Magic Pond

There is a lake, of square shape. There are four temples on each
corner. There is a flower tree next to, say temple no 1. The pond has
this magic power, if a flower is dip into the water it doubles the
quantity. There is a warning note from the priest, saying "No flower
should be wasted".

So the puzzle is, how many flowers should be plucked from the tree and
should be offered in the temple and after offering at each temple, no
flower should be left. Each temple has to be offered the same number
of flower. Before offering, flowers has to be dipped in to the pond to
get it double, as he can pluck the flowers from the tree only once, so
he has to be care-full in choosing, the total number of flowers

There are infinite solutions to this problem. Say x flowers are plucked and
y flowers are offered in each temple. then -

2(2(2(2x-y) -y) -y) -y =0
i.e.
16x-15y=0;

any pair the x and y satisfying this equation is a solution.Smallest numbers
are 15, 16

Tuesday, March 15, 2011

Splitting Chocolate Bars

Assume you have a chocolate bar consisting, as usual, of a number of squares arranged in a rectangular pattern. Your task is to split the bar into small squares (always breaking along the lines between the squares) with a minimum number of breaks. How many will it take?
Ans. N-1

The number of moves needed to break it into separate squares is invariant with regard to the actual sequence of moves.

Proof (by induction)

1. If there are just two squares we clearly need just one break.
2. Assume that for numbers 2

Proof #2
Let start counting how many pieces we have after a number of breaks. The important observation is that every time we break a piece the total number of pieces is increased by one. (For one bigger piece have been replaced with two smaller ones.) When there is no pieces to break, each piece is a small square. At the beginning (after 0 breaks) we had 1 piece. After 1 break we got 2 pieces. As I said earlier, increasing the number of breaks by one increases the number of pieces by 1. Therefore, the latter is always greater by one than the former

Monday, March 14, 2011

10 Digit Number Problem

en-digit number. Find a 10-digit number whose first digit
is the number of 1’s in the 10-digit number, whose second
digit is the number of 2’s in the 10-digit number, whose
third digit is the number of 3’s in the 10-digit number, and
so on. The ninth digit must be the number of nines in the
10-digit number and the tenth digit must be the number of
zeros in the 10-digit number


I'll denote the digits a%5B1%5D, a%5B2%5D, ..., a%5B10%5D. Since there are a%5B1%5D 1's, a%5B2%5D 2's, ..., a%5B10%5D 0's, then sum%28a%5Bi%5D%2C+i+=+1%2C+10%29+=+10 because there are ten digits in the number.

Suppose a%5B10%5D+=+0. Then a contradiction would result, since there is at least one zero, so a%5B10%5D+%3E=+1.


Suppose a%5B10%5D+=+1. Then, this implies that there is exactly one zero within the number, and all the other a%5Bi%5D's are at least one, which also creates a contradiction due to the sum of digits. This can also apply to higher values of a%5B10%5D. If a%5B10%5D+=+n, then sum%28a%5Bi%5D%2C+i+=+1%2C+9%29+=+10-n. Also, if n of the numbers { a%5B1%5D, a%5B2%5D, ..., a%5B9%5D } are zero, then 9-n of these numbers are greater than or equal to 1, and they sum up to 10-n.

By the Pigeonhole principle, exactly one of the 9-n numbers is equal to 2 and all other nonzero numbers are equal to 1 (this means, 8-n numbers equal to 1). Therefore the number has n zeros, 8-n 1's and one 2. We can easily guess and check based on the value of n, which can only range between 2 and 8. We see that the number 2100010006 satisfies all the given constraints, which happens when n+=+6.

Friday, March 11, 2011

Proability of Life or Dead

You are a prisoner sentenced to death. The Emperor offers you a chance to live by playing a simple game. He gives you 50 black marbles, 50 white marbles and 2 empty bowls. He then says, "Divide these 100 marbles into these 2 bowls. You can divide them any way you like as long as you use all the marbles. Then I will blindfold you and mix the bowls around. You then can choose one bowl and remove ONE marble. If the marble is WHITE you will live, but if the marble is BLACK... you will die."

How do you divide the marbles up so that you have the greatest probability of choosing a WHITE marble?


Hint
The answer does not guarantee 100% you will chose a white marble, but you have a much better chance.

Answer
Place 1 white marble in one bowl, and place the rest of the marbles in the other bowl (49 whites, and 50 blacks).

This way you begin with a 50/50 chance of choosing the bowl with just one white marble, therefore life! BUT even if you choose the other bowl, you still have ALMOST a 50/50 chance at picking one of the 49 white marbles.

Einstien,Newton,Jamse watt & You..Where are You ??

You find yourself half way between Tokyo and Rio de Janeiro; about as far from Mexico City as from Moscow.

Albert Einstein waits to the left, ready to explain his theory of relativity. A short distance to your right James Watt hopes to recount his development of the steam engine to you. John Steinbeck and Isaac Bashevis Singer crouch over you, vying for your attention.

Your surroundings are conventional and functional. One aspect stands out: A piece of furniture - represented in 2 or 3 specimens in the typical home - recurs here hundreds of times.

What is this piece of furniture, and where are you?
You find yourself half way between Tokyo and Rio de Janeiro; about as far from Mexico City as from Moscow.

Albert Einstein waits to the left, ready to explain his theory of relativity. A short distance to your right James Watt hopes to recount his development of the steam engine to you. John Steinbeck and Isaac Bashevis Singer crouch over you, vying for your attention.

Your surroundings are conventional and functional. One aspect stands out: A piece of furniture - represented in 2 or 3 specimens in the typical home - recurs here hundreds of times.

What is this piece of furniture, and where are you?