I am learing Binary search in C language and struggling with the this question. Given a rising array (doesn't have repeat numbers), target value and a number K, i need to find the closest value (in the array) to the target value, but according to K. For example: for array {1, 2, 3, 4, 5, 6} and target value 4. if k=1 we return 4 since it is the closest in value. if k=2 we return 3 (3,5 are equal in distance from target=4, in this case we return the value of the smaller number) if k=3 we return 5. *Target value doesn't have to be included in the array. So far i wrote this code that does basic Binary search to find where the target value roughly is. But i'm struggling with making the for loop to return value according to K, since the allowed Computational complexity is log(n)+k, i'm pretty sure i need to utilize a for loop. I would appreciate some guidance on how to continue from this point.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*******************************Defines****************************************/
#define ARR_MAX_LENGTH 50
/***************************Function declarations******************************/
/**
* @fn k_closest_to_target
* @brief Return the value of the k closest number to target in 'arr'.
* @param arr - Sorted array of uniq integers.
* @param n - Length of arr.
* @param target - Integer number.
* @param k - A non-negative number.
* @return - value of kth closest to target.
* @note If 2 different numbers are at the same distance from target we'll
* treat the smaller one to be closer to target.
* @note We assume k <= n.
* @note Time complex is O(log(n) + k).
*/
int k_closest_to_target(int arr[], int n, int target, int k);
/*************************Put functions declarations here**********************/
/******************************************************************************/
int main() {
int arr[ARR_MAX_LENGTH] = { 0 };
int n = 0;
int target = 0;
int k = 0;
printf("Please enter array length:\n");
scanf("%d",&n);
printf("Please enter target number:\n");
scanf("%d",&target);
printf("Please enter k:\n");
scanf("%d",&k);
printf("Please enter sorted and uniq array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d",&arr[i]);
}
printf("%d\n",k_closest_to_target(arr,n,target,k));
return 0;
}
/*************************Functions implementations****************************/
int k_closest_to_target(int arr[], int n, int target, int k){
int high = n-1, low = 0;
while (low <= high){
int m = low + (high-low)/2;
if (arr[m] == target)
position_of_k_relative_to_target();
if (arr[m] < target)
low = m+1;
else
high = m-1;
}
return 0;
}
int distance(int arr[],int target){
}
Use binary search to find the number in the array closest to the target (equal to the target if it exists).
Set an upper position and a lower position equal to the index of the found position.
Repeat k−1 times: Examine the array element just beyond the lower position and the element just beyond the upper position. Decide which one is closer to the target. If equal, prefer the lower position. Adjust the chosen position to be one farther away.
When done, the element at the more recently adjusted position is the value to be returned.
You will need to modify the above to account for the position trackers reaching the ends of the array.