in Algorithms edited by
21,676 views
56 votes
56 votes

Let $A$ be an array of $31$ numbers consisting of a sequence of $0$'s followed by a sequence of $1$'s. The problem is to find the smallest index $i$ such that $A\left [i \right ]$ is $1$ by probing the minimum number of locations in $A$. The worst case number of probes performed by an optimal algorithm is ____________.

in Algorithms edited by
by
21.7k views

4 Comments

1
1

@Ritwik Mishra 1

wouldn’t the 3rd probe suffice for finding?

0
0
Suppose I wanted to add another condition to the problem i.e. the array must contain atleast 3 0's.Will the no.of probes be "6"??
1
1

10 Answers

77 votes
77 votes
Best answer
Here, since $0$s are followed by $1$s so we have a sorted sequence and we can apply binary search.

At each stage we compare with ${\frac{(\text{low + high})}{2}}^{\text{th}}$ element index and if it is $1$ we check left and if it is $0$ we check right.

Total worst case no. of probes is $\lceil \log_{2}{31}\rceil=5.$

So, answer is $5.$
edited by

17 Comments

how 5 comparisions are enough to do this?
01111111111....
how can we recognize the smallet index with 5 comparisions in above example?
2
2
16-8-4-2-1 binary searching
2
2
Is no. of probes same as no. of comparisons made in this case of Binary Search?? I want to ask what is the meaning of 'probes'?
3
3

Actually not, only 4 probes are sufficient here because once you find a 1 at 2nd position, you're certain that i=2. So, this 01111.. comes under the average case.

Whereas, the sequence 001111.. or 000111.. comes under the worst case and requires the probes 16,8,4,2,3. The probe at 3 is required after finding 0 at 2nd position to ascertain whether i=3 or 4.

And by probe the question means the number of locations that the program dereferences.

1
1

 Vidhi Sethi   No not same.

0
0
Is no.of probes is the no. of times we perform binary search i.e. one binary search == one probe
2
2
Yes one iteration of binary search = one probe
20
20
I think answer should be 1 probe.                                                                                                     You can add the entire elements in the array ------------------say we got value 'x'

             then  arr[31-x] will directly give us the first slot with value '1'
0
0
it is asking for worst case
0
0

I think it should be 6 in case if the 1 is present at 31st position. 

indexes are staring from 0.

15,22,26,28,29,.....30

Now (29+30)/2 is always 29.

So we have to check the last value either at the first time or the last time forcefully. divide and conquer will not let us check the last index.

Also if the 1 is not present then also same scenario. 

 

Please justify.

 

3
3
edited by

@abhishek_tyagi Here you are not incrementing the value of i by 1. As initially i and j will be 0 and 31 respectively then k=(0+31)/2 = 15, now the index of  will be incremented to i+1 so i becomes 16. Now again calculate the new value of k, if arr[k]== 0 then go to the right or move to left.

If we consider the sequence 0000...01 as the worst-case then we would be looking at  this sequence :- 16,24,28,29,30 => 5 .

5
5
@roodramohan when you are at the position 30 and found out that 1 is not here, then definitely it will be at 31, so we don't need to do probing.It is also mentioned in the question that there is atleast one 1's is present.
2
2
> in the question that there is atleast one 1's is present.

In my opinion the question doesn't really mention this. It does say followed by a sequence of 1s which can be interpreted either way. The professors messed this one up for no apparent gain in its ability to judge a student's understanding. If only they could have mentioned assume atleast 1 of each type is present (like they do in majority of other questions)
0
0
@Aboveallplayer No only 4 probes are sufficient for the case:011111111......
0
0
3 probes sufficient here
0
0
SUDEEP I AGREE WITH YOU MAN
0
0

@Srinivas_Reddy_Kotla To find sum, algorithm need to probe all the index(i.e,. 31) So, your algorithm takes 31 probes 

0
0
25 votes
25 votes

It should not take more than 5 probes 

#include <bits/stdc++.h>
using namespace std;
#define N 100
void display(bool a[]) {
  int i=0;
  while(i<31) printf("%2d ",a[i++]);
}
void assign(bool a[],int zeros) {
  for(int i=0;i<31;i++)
    (i<zeros)?a[i] = 0:a[i] = 1;
}
int main() {
  srand(time(NULL));
  bool a[31];
  // header
  for(int i=0;i<31;i++) printf("%2d ",i);
  printf("\n\n");
  int max_probes = 0;
  for(int iteration = 1;iteration <= N;iteration++) {
    int zeros = rand()%32;
    assign(a,zeros);
    sort(a,a+31);
    int low,high,mid,ans;
    std::vector<int> seq;
    
    low = 0;
    high = 31;
    while(low < high) {
      mid = low + floor((high - low) / 2);
      seq.push_back(mid);
      if(a[mid] == 0) {
      low = mid + 1;
      ans = low;
        if(a[mid + 1] == 1) break; 
      } else {
      high = mid;
      ans = high;
      if(mid > 0)
        if(a[mid - 1] == 0) break;

      }
    }

    display(a);
    printf(" | probes=%d  ",seq.size());
    for(auto e:seq) printf("%d ",e);
    printf(" | at = %dth\n",ans);
    //if(ans == 15) printf("\nHHH=-------------\n");
    max_probes = max(max_probes,(int)(seq.size()));
    seq.clear();	
  }
  printf("%d\n",max_probes);
}

edited by
by

4 Comments

Here it is 5 as we have stopped binary search at last but one index in worst case considering input as 0000......1? I mean we have stopped probing at last but one index?
0
0
Why no one is considering the case of all zeroes?
2
2

@neel19 because in the question it is mentioned that no of o’s are more than 1 and after 0 , 1’s are following.

0
0
20 votes
20 votes

Ans is ceil(log 31) =5   Why ceil??

Take another example.
1 2 3 4 5 6 7   (7 elements... We are searching for 1)
Iteration 1:  1 2 3 4  (4!=1)     5 6 7 cancelled out.
Iteration 2:  1 2  (2!=1)          3 4 cancelled out.
Iteration 3:  1  (1==1)

3 = ceil (log 7) you know...   hence.
Here ans is  ceil (log 31) = 5        (base is 2 every where)

Here in this question, the number are sequence of zeros followed by sequence of ones.
We have to find the first index from where 1 starts. Right ?
So if you got a '1' just check if it's previous element is 0. Thats it. If it is not 0, continue binary search.
Worst Case happens when all are 1 or only first element is zero & rest are one.
111111...11  or
01111.....11  or
0011.......11  or
00011.....11  or
.
.
.

ceil(log 31) = ceil(log 30)  =  ceil(log 29) =............
only log 16 will give 4 ,   anything greator than 16 elements with ceil will give 5 as answer.

Worst case will also happen for
00.......00
00.......01
.
.
.
You got 0, here you will move right side to find the first index from where 1 starts.

edited by
by

3 Comments

Great explanation bro :-)
0
0
Great!
0
0
Indeed it’s a good question !!
0
0
9 votes
9 votes
Do Binary search.
Worst case 000000000000..01 (i.e. n-1 0s and last one is 1).
It will take log n time worst case. Hence log 31 = 5.

3 Comments

can u beleive it i made a blunder, i made it 4.95 (log 31).. should have made it 5
1
1
Actually, I do relate to that.
0
0
@aboveallplayer Probes are not in floating-point, you should have made it to 5.
1
1
Answer:

Related questions