#include <stdio.h>
#include <stdbool.h>
bool two_pointer_sum(int arr[], int size, int target) {
int left = 0;
int right = size - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == target) {
printf("Pair found: %d + %d = %d\n", arr[left], arr[right], target);
return true;
} else if (sum < target) {
left++; // increase sum by moving left pointer right
} else {
right--; // decrease sum by moving right pointer left
}
}
printf("No pair found that sums to %d\n", target);
return false;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 7;
two_pointer_sum(arr, size, target);
return 0;
}loop until pointers meet or cross
int sum = arr[left] + arr[right];
calculate sum of values at pointers
check if sum matches target
left++; // increase sum by moving left pointer right
move left pointer right to increase sum when sum too small
right--; // decrease sum by moving right pointer left
move right pointer left to decrease sum when sum too large