Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- int bin_search(int *a, int n, int x)
- {
- int low, mid, high;
- int sum_left, sum_right;
- low = 0;
- high = n - 1;
- while(low <= high)
- {
- mid = (high+low) / 2;
- sum_left = a[mid] + a[mid-1];
- sum_right = a[mid] + a[mid+1];
- if(sum_left == x)
- return mid-1;
- else if(sum_right == x)
- return mid;
- else if(sum_right < x)
- low = mid+1;
- else if(sum_right > x)
- high = mid-1;
- }
- return -1;
- }
- void main()
- {
- int a[] = {-3, 8, 1, 9, 15, 10, 18,33,21}; // 5
- int result = bin_search(a, 9, 54);
- printf("Result %d\n", result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement