GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
137 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?

  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 D.how can we apply binary search on odd and even seperatly..pls guide me

asked in DS by Veteran (13.2k points)   | 137 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 J.now if i decrment value of j by mid -2,j becomes 3.i=1 and j=3 then  mid =2 which is an even position.how 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 right..now 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 Aug 2017
  1. ABKUNDAN

    4654 Points

  2. Bikram

    4012 Points

  3. akash.dinkar12

    3136 Points

  4. rahul sharma 5

    2832 Points

  5. manu00x

    2644 Points

  6. makhdoom ghaya

    2370 Points

  7. just_bhavana

    2040 Points

  8. Tesla!

    1742 Points

  9. pawan kumarln

    1574 Points

  10. learner_geek

    1554 Points


24,864 questions
31,941 answers
74,060 comments
30,062 users