Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int binarySrc(int arr[], int low, int high) {
- int mid;
- while (low <= high) {
- mid = (low + high) / 2;
- if (arr[mid] && (!mid || !arr[mid - 1])) {
- break;
- } else if (!arr[mid]) {
- low = mid + 1;
- } else {
- high = mid - 1;
- }
- }
- return mid;
- }
- int posOfFi1(int arr[]) {
- int low = 0, high = 1;
- while (!arr[high]) {
- low = high;
- high *= 2;
- }
- return binarySrc(arr, low, high);
- }
- int main() {
- //simulate an infinite array
- int arr[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- };
- printf("idx of 1st 1: %d", posOfFi1(arr));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement