# Solutions in C

• I can provide 2 methods: doing a linear search and find the right position for insertion, or, doing a binary search to find any appropriate place to fill in.

Binary Search:

``````int searchInsert(int* nums, int numsSize, int target) {
return binarySearchInsertion( nums, 0, numsSize - 1, target );
}

int binarySearchInsertion( int *nums, int start, int end, int target ) {
int middle;

if( start < end ) {
middle = (start + end)/2;
if( nums[middle] < target )
return binarySearchInsertion( nums, middle + 1, end, target );
else if( nums[middle] > target )
return binarySearchInsertion( nums, start, middle, target );
else
return middle;
}

return target <= nums[start] ? start : end + 1;

}
``````

Linear Search:

``````int searchInsert(int* nums, int numsSize, int target) {
int i = 0;
while( i < numsSize ) {
if( nums[i] == target )
return i;
else if( nums[i] < target )
i++;
else
break;
}

return i;
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.