Exam 1
Exam 1
Instructions
Do not use hashing, unless it is mentioned in the question that you are allowed to use hashing.
No need to prove the correctness of your algorithm, unless the question explicitly asks you to
prove the correctness of your algorithm.
Every question has a page limit mentioned in square brackets against it. You are expected to
complete your answer within the allowed number of pages. The answer paper you submit should
be neat and clean. You may use separate paper(s) for doing rough work which you need not
submit.
Problem Set
1. (a) Describe an algorithm that determines whether a given set X of n integers contains two
elements whose sum is zero, in O(n log n) time. 4 marks [1 page].
(b) Describe an algorithm that determines whether a given set X of n integers contains three
elements whose sum is zero, in O(n2 ) time. 4 marks [1 page].
(c) Assume that the input set X of n integers contains only integers between −5000n and
6000n. Describe an algorithm that determines whether X contains three elements whose
sum is zero, in O(n log n) time. 8 marks [1.5 pages].
2. Describe an algorithm that sorts an input array A[1 . . . n] by calling a subroutine CubeRootSort(k),
√
which sorts the subarray A[k + 1 . . . k + 3 n] in place, given an arbitrary integer k between 0
√ √
and n − 3 n as input. (To simplify the problem, assume that 3 n is an integer.) Your algorithm
is only allowed to inspect or modify the input array by calling CubeRootSort; in particular,
your algorithm must not directly compare, move, or copy array elements. How many times
(asymptotically, no need of exact number) does your algorithm call CubeRootSort in the
worst case? 7 marks [2 pages].
3. Nitin wants to invite people for his birthday party. He knows N people. He also knows which
pairs of people in this set of N people know each other. He wants to invite as many people as
possible from these N people such that the following two conditions are met:
(a) every guest who is attending the birthday party knows at least 3 other guests who have
come, and
(b) every guest who is attending the birthday party does not know at least 5 other guests who
have come.
Describe an algorithm that finds a largest possible subset of people Nitin can invite, given a set
of N people and the list of pairs among them who know each other. Prove the correctness of
your algorithm. 7 marks [2 pages].