First time here? Checkout the FAQ!
+3 votes

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?

  1.   Sort the given array using quick sort and then apply binary search on array.
  2.   Merge the sorted lists and apply binary search.
  3.   Apply binary search on the entire array.
  4.   Separately apply binary search on the odd position elements and even position elements

i thought answer would be B,cuz then O(n) time will be taken but answer is can we apply binary search on odd and even seperatly..pls guide me

asked in DS by Veteran (14.8k points) 15 152 317 | 154 views
Separately apply binary search on the odd position elements and even position elements

it is possible and i think it is correct

can you pls tell how will we apply seperatly on odd and even positions..?

1 2 3 4 5 6 7 8 9 10
25 1 17 5 13 10 9 23 6 30


suppose we want to search 25,then we will run 2 binary searches.

one from (1-9)i=1 and j=9 for odd and other for (2-10) for even.

now,lets take odd case,mid = 5 which is an odd position.25 > 13 ,so we should decrement value of if i decrment value of j by mid -2,j becomes 3.i=1 and j=3 then  mid =2 which is an even should i go about it now?


first count how many even inices are there

in your example it is 5 {2,4,6,8,10}

aplly binary search on range 1 to 5

during that multiplyeach index with 2 to get original index

similar do with odd index
alright..thanks !
but i did nt get one thing,in my example

i find mid =3 here then 3*2 =6 index a[6] =10 < 25,so i should move should i increment i by 1 or 2..?incrmenting 1 will give i=7 and inc 2 will give i=8

Please log in or register to answer this question.

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

    23642 Points

  2. Bikram

    17188 Points

  3. Habibkhan

    8734 Points

  4. srestha

    6404 Points

  5. Debashish Deka

    5478 Points

  6. jothee

    5098 Points

  7. Sachin Mittal 1

    4882 Points

  8. joshi_nitish

    4478 Points

  9. sushmita

    4008 Points

  10. Rishi yadav

    3960 Points

Recent Badges

Popular Question Bikram
Regular Mr.OOPs
Avid Reader Venkat Sai
Popular Question jothee
Verified Human anchal Singh
Great Question Kathleen
Verified Human Ankita_ Pawar
Avid Reader kenzou
Notable Question Sumit Chaudhary 2
Nice Comment rahul sharma 5
27,380 questions
35,231 answers
33,389 users