Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/167.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Return an array of size *returnSize containing the 1-based indices of two numbers
* that add up to the target.
* Note: The returned array must be malloced, assume caller calls free().
*/
#include <stdlib.h>
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
*returnSize = 2;
int* output = (int*)malloc(2 * sizeof(int));

int left =0;
int right = numbersSize-1;
while(left<right){
int sum = numbers[left] + numbers[right];
if(sum == target){
output[0] = left+1;
output[1] = right+1;
return output;
}
else if(sum<target){
left++;
}
else{
right--;
}
}
return NULL;
}