edited by
4,377 views
26 votes
26 votes

Suppose you are given an array $A$ with $2n$ numbers.

The numbers in odd positions are sorted in ascending order, that is, $A[1] \leq A[3] \leq \ldots \leq A[2n - 1]$.

The numbers in even positions are sorted in descending order, that is, $A[2] \geq A[4] \geq \ldots \geq A[2n]$.

What is the method you would recommend for determining if a given number is in the array?

  1. Sort the array using quick-sort and then use binary search.
  2. Merge the sorted lists and perform binary search.
  3. Perform a single binary search on the entire array.
  4. Perform separate binary searches on the odd positions and the even positions.
  5. Search sequentially from the end of the array.
edited by

1 Answer

Best answer
37 votes
37 votes

Option D is the correct answer.

We can simply use clever indexing to binary search the element in the odd positions, and in the even positions separately.

This will take $O(\log n)$ time and $O(1)$ space in the worst case.

A: Sorting using Quicksort will take $O(n^2)$ time.
B: Merging will take $O(n)$ time and $O(n)$ space.
C: Binary search only works on a sorted array.
E: Sequential search will take $O(n)$ time.

edited by
Answer:

Related questions