-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq11)findstartingindexof1.c
61 lines (57 loc) · 1.11 KB
/
q11)findstartingindexof1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//q11)given an array of size unknown will all 0's in the begining and 1's in the end. find the starting index of 1
//approach1,2:O(n),O(logn)
#include <stdio.h>
#include<math.h>
int i,c,t;
int j=0;
int l,r,m;
void bs(int *arr,int l,int r)
{
//printf("%d\t%d",l,r);
while(l<=r)
{
m=l+(r-l)/2;
// printf("m=%d",m);
if(arr[m]==1&&arr[m-1]!=1)
{
printf("index=%d",m);
}
if(arr[m]==0)
{
l=m+1;
}
else
r=m-1;
}
}
int fun(int *arr,int l,int n)
{
for(i=0;(c=pow(2,i))<=n;i++)
{
if(arr[c-1]==1)
{
printf("holaindex=%d",c-1);
// return 0;
}
t=c;
}
// printf("c=%d,%d",t,c);
if(t<=n)
{
//apply bs from c to n to find the position of starting index of 1
bs(arr,t,n);
}
// printf("%d\t%d",c,j);
if(j<=n)
{
// fun(arr,j/2,n);
}
return 0;
}
int main(){
//1.check at every 2^k indexed elements
int arr[]={0,0,0,0,0,0,0,0,0,0,0,1,1,1,1};
int n=sizeof(arr)/sizeof(arr[0]);
fun(arr,0,n-1);
return 0;
}