My issue
why my code shows compilation error
My code
#include <stdio.h>
// Function to perform binary search and find the index or insertion point
int search_insert_position(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid; // Target found
} else if (arr[mid] < target) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}
return low; // Return insertion position if not found
}
int main() {
int n, q;
// Reading input values for n (number of elements) and q (number of queries)
scanf("%d %d", &n, &q);
// Dynamically allocate memory for the array based on n
int arr[n];
// Input sorted array
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Handle each query
for (int i = 0; i < q; i++) {
int target;
// Read the target element for the query
scanf("%d", &target);
// Output the index or insertion point
int result = search_insert_position(arr, n, target);
printf("%d\n", result);
}
return 0;
}
Learning course: Data structures & Algorithms lab
Problem Link: Problem in Data structures & Algorithms lab