-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon2.c
55 lines (51 loc) · 836 Bytes
/
amazon2.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
#include<stdio.h>
int found(int a[],int n,int k)
{
int low,high=n,mid;
low=1;
if(n%2==0)
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
//printf("%d %d %d\n",low,high,mid);
if(mid%2==0)
mid--;
if(a[mid]==k)
return mid;
else if(a[mid]>k)
high=mid-2;
else
low=mid+2;
}
low=2,high=n;
if(n%2!=0)
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
//printf("%d %d %d\n",low,high,mid);
if(mid%2!=0)
mid--;
if(a[mid]==k)
return mid;
else if(a[mid]>k)
high=mid-2;
else
low=mid+2;
}
return 0;
}
int main()
{
int n,a[100010],i,k,loc=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&k);
if((loc=found(a,n,k)))
printf("found at position %d\n",loc);
else
printf("Not found\n");
return 0;
}