edited by
1,647 views
6 votes
6 votes
I/p - array of n element in which untill some postion all are integer and afterward all are star (*)

 

O/p- find the postion of 1st star (*)

Hint - if lenear search is possible the go to BS

Find time complexity ..?
edited by

3 Answers

16 votes
16 votes
This problem can be solved by using Binary Search algorithm. Hence time complexity will be $ O(log n) $.

Strategies will be like this

1) Get the midpoint, and check the value at it

2) If the value is integer then directly move right

3) else if the value is a star then check whether it has integer at the immediate left position, if yes then print the location of the star, its the answer, else move to the left.
1 votes
1 votes

Linear search is possible.

To solve it from binary search

1. It is the same case when we are applying binary search for an absent element in a sorted array.

2. Here difference is we have to search a *.

3. Solve it by recursion. Find the mid point by low+high/2. If mid point = any number then make low=mid point. And if mid point= * then make high=mid point.

Function (low,high,array[ ])

T(n)=T(n/2)+1

4. In ever recursion check if(high=low+1) then index of first * is index of high.

Complexity O(logn)

1 votes
1 votes
(logn).... We can search it by binary search tree ... First it goes in multiples of 2 and checks its left and right values...

Related questions

4 votes
4 votes
4 answers
2
Parshu gate asked Nov 27, 2017
6,791 views
How many different binary search trees can be constructed using six distinct keys? 256 128 132 264
0 votes
0 votes
2 answers
4
dhruba asked Jun 5, 2023
1,151 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). ...