GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
120 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)   | 120 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 Jun 2017
  1. Bikram

    3694 Points

  2. Hemant Parihar

    1484 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1408 Points

  5. Niraj Singh 2

    1311 Points

  6. Rupendra Choudhary

    1194 Points

  7. rahul sharma 5

    1120 Points

  8. Arjun

    930 Points

  9. srestha

    928 Points

  10. Debashish Deka

    896 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1950 Points

  2. Niraj Singh 2

    1306 Points

  3. junaid ahmad

    502 Points

  4. sudsho

    410 Points

  5. akankshadewangan24

    388 Points


23,355 questions
30,065 answers
67,365 comments
28,382 users