First time here? Checkout the FAQ!
+1 vote
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 (6.5k points) 8 115 247 | 390 views
can someone give me code and recurrence relation for this?

2 Answers

+5 votes

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 (88.6k points) 15 58 294
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
0 votes
what i whould recommend is that seperate the give array into two arrays

1 elements in odd positions in non - increasing order

2. elements in even position in non - decreasing order

seperate this two arrays and store them in wo seperate arrays

and run the binary search on the both arrays  the time complexity will be

time to seperate and store in different arrays is n  (as they are n elements )

binary search (logm+logm)
answered by Active (2.2k points) 2 18 36

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23398 Points

  2. Bikram

    17078 Points

  3. Habibkhan

    8280 Points

  4. srestha

    6300 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4352 Points

  9. sushmita

    3970 Points

  10. Rishi yadav

    3804 Points

Recent Badges

Popular Question sh!va
Popular Question sh!va
Regular Rishabh Gupta 2
Popular Question Sunil8860
Reader Rajesh Veeranki 2
Notable Question rahul sharma 5
Commentator Shivam Chauhan
Notable Question set2018
Nice Comment srestha
Notable Question set2018
27,325 questions
35,177 answers
33,280 users