Complete the code to find the floor of a target in a sorted array.
int floorInSortedArray(int arr[], int n, int target) {
int low = 0, high = n - 1, floor = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
floor = mid;
low = [1];
} else {
high = mid - 1;
}
}
return floor;
}When arr[mid] is less than target, we move low to mid + 1 to search the right half.
Complete the code to find the ceil of a target in a sorted array.
int ceilInSortedArray(int arr[], int n, int target) {
int low = 0, high = n - 1, ceil = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
ceil = mid;
high = [1];
} else {
low = mid + 1;
}
}
return ceil;
}When arr[mid] is greater than target, we move high to mid - 1 to search the left half.
Fix the error in the binary search loop condition to avoid infinite loops.
while ([1]) { int mid = low + (high - low) / 2; // rest of code }
The loop should run while low is less than or equal to high to cover all elements.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
int lengths[] = {0};
for (int i = 0; i < n; i++) {
if ([1] > 3) {
lengths[i] = [2];
}
}We check the length of each word and assign its length to lengths array.
Fill all three blanks to create a map of keys to values where values are positive.
typedef struct {
char key[20];
int value;
} Pair;
Pair result[100];
int count = 0;
for (int i = 0; i < n; i++) {
if (data[i].[1] > 0) {
strcpy(result[count].[2], data[i].[3]);
result[count].value = data[i].value;
count++;
}
}Check if data[i].value is positive, then copy data[i].key to result[count].key.
