Q1: Show that \sum_{i=1}^n i^2 is in O(n^3) Sol: let's prove by induction on n that \sum_{i=1}^n i^2 <= n^3 for n>=1 - do the basis (easy) - Ind. Hyp: true for all values up to k>=1. for n=k+1: \sum_{i=1}^{k+1} i^2 = \sum_{i=1}^k i^2 + (k+1)^2 <= k^3 + k^2 + 2k + 1 <= k^3 + 3k^2 + 3k + 1 = (k+1)^3 Q2: Suppose you are given a set of small boxes, numbered 1 to n, identical in every aspect except that each of the first i contains a pearl whereas the remaining n-i are empty. You can have two magic wands that can each test if a box is empty or not in a single touch, except that a wand disappears if you test it on a box that is empty. Show that, without knowing the value of i, you can use the two wands to determine all the boxes containing pearls using at most no more than 2\sqrt{n} wand touches. Sol: We will fix number k later. The idea is first use one wand on boxes 1, k, 2k, 3k, ... The smallest i for which the wand burns on box i*k indicates that the first empty box is among (i-1)*k+1....i*k Now we use the second wand sequentially from (i-1)*k+1 to i*k-1 to find it. The total number of toches will be at most: n/k + k n/k is the number of boxes for the first wand and k for the second one If we now choose k to be (about) \sqrt{n} then we have n/\sqrt{n}+\sqrt{n} which is in O(\sqrt{n}) touches. Q3: LOOP INVARIANT EXAMPLE Prove the following pseudocode is correct, i.e., returns, max_{i in 1..length(A)-1} |A[i] - A[i+1]| DIFF(A) d = |A[1] - A[2]| for i = 2 to length(A)-1 do if (|A[i] - A[i+1]| > d) d = |A[i] - A[i+1]| return d Proof: Loop invariant is, d = max_{i in 1..i-1} |A[i] - A[i+1]| We proved initialization, maintenance, and showed the loop invariant at termination proves correctness