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:
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.
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:
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,
Why is 1) hard?
A) Read the first value. 3
B) Put it in both places of your MaxMin vector [3,3]
A) Read the next value. 7
B) If it exceeds MaxMin(1), replace it [7,3] <== Replacement
C) If it exceeded by MaxMin(2), replace it [7,3] <== No action
Ask your self what is difficult about:
Originally Posted by Algohelp
Then let us know and we can address your real problem
read n, A[1..n]
lo = maxint
hi = minint
for idx=1 to n
Thanks for the help CaptainBlack,
Regarding the first solution I can see exactly what it wants me to write, and I can write most of it, but I never feel very confident with my solution once I've written it. I still have a few problems with basics. Anyway as I say I really didn't put anytime in the module and some of the trivial questions seem harder than they should without someone to point out the obvious, so that's why I posted those up just to work through as a starting point. Have a retake in a few days and I'm gonna attempt some other questions and post the answers up here. Would appreciate if you could tell me if they're right or wrong when I do!