## 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.