Group A
Group A
Group A
Part 1
A.2 You are given an unsorted array A = (a1 , a2 , a3 , . . . , an ) of n integers (positive, negative, or zero). Your
task is to find a non-empty contiguous subsequence ai , ai+1 , ai+2 , . . . , a j of A such that ai + ai+1 +
ai+2 + · · · + a j = 0. Report failure if no such subsequence exists. Propose an expected O(n)-time
algorithm to solve this problem. (10)
A.3 You have n currencies c1 , c2 , . . . , cn . The exchange rates of the currencies (positive floating-point
numbers) are given in a two-dimensional (n × n) table R, that is, R[i, j] stores the amount of currency
c j you get by selling one unit of currency ci . For every two currencies ci , c j , you are required to find
the sequence of distinct currencies ci , ci1 , ci2 , . . . , cik−1 , cik , c j for which the compound exchange rate
is as large as possible. Here k = 0 is allowed, standing for the situation that you directly covert ci to
c j . Assume that you cannot make a profit by a cycle of exchanges, that is, for every choice of distinct
indices j1 , j2 , . . . , jt , we have
R[ j1 , j2 ] × R[ j2 , j3 ] × · · · × R[ jt−1 , jt ] × R[ jt , j1 ] < 1.
Reduce this problem to a problem on graphs. Mention how you solve the graph problem (along with
why you need the condition that the compound exchange rate of a cycle of exchanges is < 1). What
is the running time of your algorithm? (5 + 3 + 2)
— Page 1 of 2 —
Part 2
A.4 Let n be an odd positive integer. Write a C program that will print the edges and the diagonals of a
diamond for a given value on n. For example, the diamond patterns for n = 7, n = 9, and n = 11 are
as follows. (10)
*
* ***
* *** * * *
*** * * * * * *
* * * * * * * * *
******* ********* ***********
* * * * * * * * *
*** * * * * * *
* *** * * *
* ***
*
n=7 n=9 n = 11
A.5 Write a recursive C function int countSubstr(char* str, . . .) to count the number of substrings
of a given C string, where the substrings start and end with the same character and have length of more
than one (single character substrings are not counted). You can include additional arguments in the
function as per your requirement. As an example, if the input is csedept, then the output would be
one (only one substring ede that starts and ends with the same character e and having length more
than one). (10)
— END —
— Page 2 of 2 —