Dec 30, 2008

A sorted integer Array[1...N], the range is from 1...N+1. a number is missing, find out it

A sorted integer Array[1...N], the range is from 1...N+1. a number is missing, find out it

Sorted integer array uses binary search O(logn)

int foo(int* a, int low, int up) //note the index is from 1 to N
{
if(low=up-1) return a[low]+1;
int mid=(low+up)/2;
if(a[mid]>mid) up=mid;
else low=mid;
foo(a,low,up);
}

No comments:

Post a Comment