forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path167-Two-Sum-II.c
More file actions
28 lines (25 loc) · 740 Bytes
/
167-Two-Sum-II.c
File metadata and controls
28 lines (25 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Given a 1-indexed sorted int array & target:
Return indices (added by 1) of 2 nums that add to target
2 pointers, outside in, iterate i/j if sum is too low/high
Time: O(n)
Space: O(1)
*/
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
*returnSize = 2;
int* ans = malloc(sizeof(int)*2);
int i = 0;
int j = numbersSize-1;
int k;
while (true) {
k = numbers[i]+numbers[j]-target;
if (k==0) { // numbers[i]+numbers[j] = target
ans[0] = i+1;
ans[1] = j+1;
return ans;
} else if (k>0) // numbers[i]+numbers[j] > target
j--;
else // numbers[i]+numbers[j] < target
i++;
}
}