Final (AY21/22)
Last updated
Last updated
Question paper without answers:
Question paper with answers:
For long
variables, it can be negative! Always try to think about negative number when trying to form the counter example.
The keyword "if and only if" needs to prove both forwardly and reversely!
The code below is a classic memory allocation error:
Here a
should be a pointer pointing to an array of long
, but here you assume it should be pointing to to an array of long *
. The implementation above is wrong. The correct one should be
The two recurrence relation below has the same Time Complexity:
And the time complexity is
A good question to try practicing!
To get the strong and correct loop invariant in this question is a bit tricky. In comments, you can clearly see if you use the weak but correct loop invariant a[l] < a[r+1 .. len-1] || a[r] <= a[0 .. l-1]
. It will be impossible to do the next question.
So, I would say the tip I get to do this kind of question is that:
Try to find a counter example to prove the given loop invariant is incorrect
To find the correct one, think more about what the code is doing rather than giving out the exact expression directly because sometimes the loop invariant even cannot be expressed in the exact expression. e.g. in this problem, the strong and correct loop invariant is "the minimum element of the array is in a[l .. r]
"