Y16-2119-assign2.pdf - COMP 2119A Introduction to Data Structures and Algorithms Assignment Two Due Date 25 October 2016 midnight Assignment box A3(also
Y16-2119-assign2.pdf - COMP 2119A Introduction to Data...
COMP 2119A Introduction to Data Structures and Algorithms Assignment Two Due Date: 25 October 2016 midnight Assignment box: A3 (also submit a scanned copy to moodle) [Note that for the questions involving algorithm design, before you present your algorithm, try to describe your idea first! More marks will be given to faster algorithms.] ** NO LATE ASSIGNMENT will be accepted ** 1) [10%] Design a recursive algorithm to solve each of the following problems and analyze the time complexity of your algorithm. (a) Given an array of nnumbers, reverse the entries in the array and you are only allowed to use constant amount of extra storage (i.e., you cannot use an extra array.). (b) Given a sortedarray of distinctintegers A[1..n] in increasing order, find out whether there is an index ifor which A[i] = 2) [10%] Given an array A[1..n] of nintegers representing the stock price of ndays, the problem is to find i, jsuch that A[j] – A[i] gives us the maximum over all possible iand j(i.e., if we buy at i-th day and sell at j-th day, we earn the most). Your algorithm should also report the maximum value of A[j] – A[i(a) Present the brute-force approach (i.e., trying all possible i, jand report the maximum of A[j] – A[i]) and analyze the time complexity of your algorithm. (b) Design a recursive algorithm to solve the problem and analyze the time complexity of your algorithm. 3) [15%] Consider the following variation of Tower of Hanoi. There are 4 pegs (0 to i. ].