refx_prog

download refx_prog

of 4

Transcript of refx_prog

  • 8/8/2019 refx_prog

    1/4

    1) Digital Stream (1.5)

    A Digital Stream is an infinite sequence, where each term is the sum of the previous term and the sum ofthe individual digits in it.The first term is known as the seed.

    Tn+1 = Tn + Sum of digits( Tn ) { for n>0 }

    and T0 = Seed. { Seed >0 }

    For Ex. a digital stream with seed 9 : 9, 18 (=9+9) , 27 (=18+1+8), 36 (=27+2+7), ...

    Now given 2 digital streams we would like to know if they intersect or not (within a finite range say 0 -4294967296).

    Input : Seeds of 2 Digital Streams.Output : Intersection point or -1 if they dont.

    2) Burning Forest (1)

    A rectangular grid is laid out over a forest, by the wild life reserve department for easy management.Some of these grid blocks contain dense population of trees, while others are barren.If a fire starts in any of the densely forested grid block, it consumes every tree in that block, and spreadsto all adjacent forested block, and continues so forth. The fire dies out once it has consumed all theforested block it could spread to from the initial block.

    Given a grid, and the block from where the fire originated, we would like to see at what state thefire dies off, and how many times it spread from one forested block to another adjacent forested block.

    Input : Dimensions of the Grid, The layout of the grid (0=>forested area, 1=>empty area),Coordinates of the block from where fire started.

    Output : Final Status of the Grid & no. of times the fire spread from one block to another.

    3) Paths (2.5)

    A traveler is on his way across the city.He starts from one landmark, and makes his way to another, and then another until he reaches his finaldestination. An arbitrary (but finite number) of landmarks lie between the starting landmark and thedestination. Direct routes may not be present between every pair of landmarks. Also the traveler will notvisit a landmark more than once.

    Given a layout of landmarks, we would like to know all possible paths the traveler might take froma specified origin to the destination and the number of such paths.

    Input : The number of landmarks, the layout in appropriate format, origin, destination

    N (total no. of land marks, 0

  • 8/8/2019 refx_prog

    2/4

    4) Bishops Reach (1.5)

    Consider a 8X8 chess board numbered in the traditional way : A to H in the horizontal axis (fromlt. to rt), and 1 to 8 in the vertical axis (from bottom to top).

    A Bishop can move diagonally to any square in the chess board.

    Given a source square, we would like to know the number of moves the Bishop needs to move tothe specified destination square, if possible.

    Input : T (no. of test cases ), followed by T lines of s ource and destination pairs (eg. A1 B3).Output : Number of moves required or -1 if its unreachable for each such pair.

    5) Ugly Numbers (2.5)

    A number is said to be ugly if its only prime factors are 2,3, or 5.

    For Ex. : 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 are the first 11 ugly numbers. By convention, 1 is also included.

    Now suppose we have a number say 123, which has 2 positions between the 3 digits. In these 2 positionswe can choose to place either a +, - or nothing to get different arithmetic expressions like 1+2+3 or12+3 or 12-3. These expressions evaluate to 6,15,9 respectively which are all Ugly numbers. But anexpression like 1-23 (= -22), or 123 itself yield non Ugly numbers.

    Given any number, we would like to know how many of the (possible) expressions (generated in theabove fashion) yield an Ugly number, and what are those expressions.

    Input : NOutput : Number of ugly numbers generated & the corresponding expressions for N.

    6) Alpha Codes (2.5)

    Two spies want to communicate secretly. They had hastily designed a code, where a word is converted toa number by replacing each letter by its equivalent numerical value (A takes the value 1 and Z 26).For ex. abc or ABC is encrypted to 123.

    But this scheme is inherently ambiguous as some encrypted message can be decrypted in multiple ways.For Ex. : 25114 can be decrypted as bean,beaad,yaad,yan,ykd,bekd (ignoring case).

    Given an encrypted message we would like to know how many different decryptions are possible.

    Input : An encrypted word.Output : Number of possible decryptions & the corresponding decryptions or -1 if its impossible to

    decode.

    7) Word Ring (3.5)

    You want to figure out the combination to an archive your kid brother has left in your PC. Knowinghim you can probably guess the password. You know he likes to play with different anagrams of wordsand line up the words such that they form a ring where the last character of each word is the first one ofthe next.Given a list of (your brothers favorite words) and the number of words he will (probably) use as thepassword , generate all possible anagram rings as mentioned above.

  • 8/8/2019 refx_prog

    3/4

    INPUT: N (total no.of words)M (no. of words to use in password)List of N words (1 per line)

    OUTPUT:All possible password sequences (1 per line) and the number of such possible passwordsformed as mentioned above.

    8) Bridge (3.5)

    A group of n people wish to cross a bridge at night. At most two people may cross atany time, and each group must have a flashlight. Only one flashlight is available amongthe n people, so some sort of shuttle arrangement must be arranged in order to returnthe flashlight so that more people may cross.Each person has a different crossing speed; the speed of a group is determined by thespeed of the slower member. Your job is to determine a strategy that gets all n peopleacross the bridge in the minimum time.

    INPUT :The input begins with a single positive integer on a line by itself indicating the numberof test cases, followed by a blank line. There is also a blank line between each twoconsecutive test suite.

    The first line of each case contains n, followed by n lines giving the crossing timesfor each of the people. There are not more than 1,000 people and nobody takes morethan 100 seconds to cross the bridge.

    OUTPUT :For each test case, the first line of output must report the total number of secondsrequired for all n people to cross the bridge. Subsequent lines give a strategy for achievingthis time. Each line contains either one or two integers, indicating which personor people form the next group to cross. Each person is indicated by the crossing timespecified in the input.Note that the crossings alternate directions, as it is necessary to return the flashlightso that more may cross. If more than one strategy yields the minimal time, any onewill do.The output of two consecutive cases must be separated by a blank line.

    9) Ones (2.5)

    Given any integer 0 n 10, 000 not divisible by 2 or 5, some multiple ofn is a number which in decimalnotation is a sequence of 1s. How many digits are in the smallest such multiple ofn?

    INPUT :Multiple lines of integers at one integer per line.

    OUTPUT :Each output line gives the smallest integerx > 0 such thatp is a number consisting of only 1s,such thatp = a b, and b is an integer greater than zero, where a is the corresponding input integer.Sample Input3

    7Sample Output36

  • 8/8/2019 refx_prog

    4/4

    10) Factovisors (2)

    The factorial function, n! is defined as follows for all non-negative integers n:0! = 1 and n! = n (n 1)! (n > 0)We say that a divides b if there exists an integerksuch that k a = b

    INPUT:The input to your program consists of several lines, each containing two non-negativeintegers, n and m, both less than 2^32.

    OUTPUT: For each input line, output a line stating whether or not m divides n!, in the formatshown below.

    10000 divides 20!

    100000 does not divide 20!

    1009 does not divide 1000!

    11) Stern-Brocot Numbers (2)

    The Stern-Brocot tree is a beautiful way for constructing the set of all non-negative fractions m/nwhere m and n are relatively prime. The idea is to start with two fractions 0/1 & 1/0and then repeat the following operation as many times as desired:

    Insert (m1+m2)/(n1+n2) between two adjacent fractions m1/n1 and m2/n2.For example, the first step gives us one new entry between 0/1 and 1/0 :0/1, 1/1, 1/0and the next gives two more:0/1,1/2,1/1,2/1,1/0, The next gives four more:0/1, 1/3, 1/2, 2/3, 1/1, 3/2, 2/1, 3/1, 1/0The entire array can be regarded as an infinite binary tree structure whose top levelslook like this

    We can regard the Stern-Brocot tree as a number system for representingrational numbers, because each positive, reduced fraction occurs exactly once. Let us

    use the letters L and R to stand for going down the left or right branch as weproceed from the root of the tree to a particular fraction; then a string of Ls and Rsuniquely identifies a place in the tree. For example, LRRL means that we go left from 1/1down to 1/2, then right to 2/3, then right to 3/4, then left to 5/7.1/1 is denoted by I

    INPUT: T (no. of test cases); followed by fractions of the form m/nOUTPUT : The Stern Brocot representation for each input fraction in a new line.