Friday, November 13, 2009

interesting algorithmic problems and puzzles

THESE QUESTIONS ARE TAKEN FROM INTERVIEWS OF GOOGLE,YAHOO,MSFT AND ADOBE.
THE QUESTIONS ARE LITTLE BIT ALTERED INORDER NOT TO BREAK THE NDA.

TRY SOLVING THESE WITH A PEN AND PAPER.

1.There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].

Solve it without division operator and in O(n).

2.Classical josephus problem-there are N numbers numbered from 1 to N and there is game to b played tht startin from 1 every second num wud b eliminated .... perform ths untill thr wud b only single num remained ...
e.g.
thr r 5 nums
1 2 3 4 5
thn aftr 1st iteration 1 3 5 wud remained
thn .... thn 1 will b elliminated and thn 5
3 will remain alone...

give sum efficient algo to calculate which num will remain at the end

3.the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4; the contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.Write some good code to find this maximum sum.

4.Consider a String with N (say 1000k) words in it.Write a function which takes two parameters i,j<=N which swaps the position of i th and the j th words present in the string.
Ex. This blog is really rocking courtesy citians.
if input if 2,5 thn o/p is
This rocking is really blog courtesy citians.

5.A convex polygon hull is given . How would you search a point ( whether a vertex or not)
in O( log n). n = number of vertices in polygon.

6.Write some efficient pseudocode to find the 6th maximum element from an array of numbers.

7.Try designing a moore/mealy machine to output the exor of the input bits supplied.

Friday, September 25, 2009

1.pouring water

Given two vessels, one of which can accommodate a litres of water and the other - b litres of water, determine the number of steps required to obtain exactly c litres of water in one of the vessels.

At the beginning both vessels are empty. The following operations are counted as 'steps':

emptying a vessel,
filling a vessel,
pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.
Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t sets of input data, each consisting of three positive integers a, b, c, not larger than 40000, given in separate lines.

Output

For each set of input data, output the minimum number of steps required to obtain c litres, or -1 if this is impossible.

Example

Sample input:
2
5
2
3
2
3
4
Sample output:
2
-1



2.piggy bank

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.

Output

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

Example

Sample Input:

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample output:

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

Wednesday, September 16, 2009

These problems are very interesting and very much researched for much efficient solutions.
Try your hand at them.



1. Maximum Value Contiguous Subsequence. Given a sequence of n real numbers A1 . . .An, determine
a contiguous subsequence Ai . . .Aj for which the sum of elements in the subsequence is maximized.

2. Making Change. You are given n types of coin denominations of values v1 <>
integers). Assume v1 = 1, so you can always make change for any amount of money C. Give an algorithm
which makes change for an amount of money C with as few coins as possible.

3. Longest Increasing Subsequence. Given a sequence of n real numbers A1 . . .An, determine a subsequence
(not necessarily contiguous) of maximum length in which the values in the subsequence form a
strictly increasing sequence.

4. Box Stacking. You are given a set of n types of rectangular 3-D boxes, where the ith box has height hi,
width wi and depth di (all real numbers). You want to create a stack of boxes which is as tall as possible,
but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are
each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that
any side functions as its base. It is also allowable to use multiple instances of the same type of box.

5. Integer Knapsack Problem (Duplicate Items Forbidden). This is the same problem as the example
above, except here it is forbidden to use more than one instance of each type of item.

Monday, May 18, 2009

algorithms

This blog is especially for improving the algorithmic skills of students of coimbatore institute of technology