Hi guys,

Took an exam back in June which dealt mainly with algorithms, however I never really put the time into the module so the exam was beyond me at times. Was just wondering if anyone could spare the time to check out a few of the questions I came up against, as I've got a retake around the corner and can't seem to get my head around them. Probably very trivial to you guys!

Ok, here it goes..

1) Write an algorithm which calculates both the maximum and minimum of a non-empty array A[1...n]. Examples of its application:

Input:

[3,7,0]

[10,11,2,1]

Output:

7 0

11 1

When writing the algorithm you are allowed to use only once occurrence of a looping construct; that is one for loop or once occurrence of a while loop.

and,

2) Given a non-empty array of numbers A[1....n], the running max of A is an array M[1...n], in which the slot M[i] contains the maximum of the subarray A[1...i]. Examples:

Input: A

[10,7,2,11]

[9]

[3,2,6,5,1]

Output: M

[10,10,10,11]

[9]

[3,3,6,6,6]

Write an algorithm, in the complexity class Θ(n), which inputs an array A[1....n] and outputs the running max of A.

Also, why is the algorithm in class Θ(n).

Any kind of help would be much appreciated,

Cheers!