GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
254 views
Consider an array ‘A’ with 2m elements. The elements in odd position are sorted in non-increasing order that is A[1] >= A[3] >= A[5]......A[2m-1] The elements in even position are sorted in non-decreasing order, that is A[2]<= A[4] <= A[6].....A[2m]. Which of the following method is recommended for finding if a given number is in array?
asked in Algorithms by Boss (6k points)   | 254 views
can someone give me code and recurrence relation for this?

1 Answer

+5 votes
Best answer

The proper way to do is :

a) Implement binary search by a little modification in finding the middle index for both the 2 same sized subarrays..

b) If found , then report the element else continue recursively..

So the time complexity will be O(logm + logm)  = O(2logm) = O(logm) 

answered by Veteran (65k points)  
selected by
Yes, also in iterative version of binary search, we need to we need to increase (or) decrease index of mid by factor of $2$
What would be the space complexity if we will apply binary search on odd indices and even indices separately?
space complexity is still O(m) exact i can say 2m +m(store half) +m(store half) = 4m
To find space complexity of recursive procedure , we consider the height of the corresponding recursion tree..As the recursion tree of binary search will contain O(logn) levels whatever the case may be so the space complexity due to function call will be : O( logn)
(logn) is extra space right ?
Yup space required for function call overhead
Top Users Feb 2017
  1. Arjun

    5278 Points

  2. Bikram

    4230 Points

  3. Habibkhan

    3942 Points

  4. Aboveallplayer

    3086 Points

  5. Debashish Deka

    2378 Points

  6. sriv_shubham

    2308 Points

  7. Smriti012

    2236 Points

  8. Arnabi

    2008 Points

  9. sh!va

    1672 Points

  10. mcjoshi

    1660 Points

Monthly Topper: Rs. 500 gift card

20,856 questions
26,009 answers
59,671 comments
22,107 users