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 (12.3k points)   | 108 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.

Top Users Mar 2017
  1. rude

    4018 Points

  2. sh!va

    2994 Points

  3. Rahul Jain25

    2804 Points

  4. Kapil

    2608 Points

  5. Debashish Deka

    2104 Points

  6. 2018

    1414 Points

  7. Vignesh Sekar

    1336 Points

  8. Bikram

    1218 Points

  9. Akriti sood

    1186 Points

  10. Sanjay Sharma

    1016 Points

Monthly Topper: Rs. 500 gift card

21,445 questions
26,757 answers
22,954 users