Question: [10%] We have k light bulbs, with labels from 1 to k. All light bulbs are off initially. At each round, we will select some
[10%] We have k light bulbs, with labels from 1 to k. All light bulbs are off initially. At each round, we will select some light bulbs to switch on/off (i.e., from on to off or from off to on) according to the following rule. At the i-th round (i = 1, 2, ..., k), We select the light bulbs whose labels are multiple of i, and we are interested to know how many light bulbs are on after the k-th round. We can compute the solution using the following code: Initialize A[1..k] = 0 (0: off, 1: on); for (round i = 1, 2, ..., k) { for (label j =i, 2i, 3i, ...) { if (sk) A[] = abs(A[i]-1); // abs - absolute value else break; return(the number of l's in A); (a) [5%] Show that the above code runs in (k log k) time. If you can show that the above code runs in O(k) time, you can get partial credits. (b) [5%) Design a faster algorithm that can solve the same problem in O(k) time
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
