/* find the next value * return: -1 means no solution, otherwise the solution */ intsolve(int* a, int arr_len, int val){ int l, r; l = 1; r = arr_len; // arr_len will never be taken.If l ends at arr_len, there is no such value while(l<r){ int mid = (l+r)>>1; if (satisify(a[mid])){ r = mid; }else{ l = mid+1; } } if (l == arr_len){ return-1; } return a[l]; }
/* find the previous value * return: -1 means no solution, otherwise the solution */ intsolve(int* a, int arr_len, int val){ int l, r; l = 0; // 0 will never be taken.If l ends at 0, there is no such value r = arr_len-1; while(l<r){ int mid = (l+r+1)>>1; if (satisify(a[mid])){ l = mid; }else{ r = mid-1; } } if (l == 0){ return-1; } return a[l]; }