#include <stdio.h>
void printPairs(int arr[], int arr_size, int sum)
{
int front = 0;
int back = arr_size - 1;
while (front < back) {
if (arr[front] + arr[back] < sum) {
front++;
} else if (arr[front] + arr[back] > sum) {
back--;
} else {
printf ("Pair with given sum %d is (%d, %d) \n", sum, arr[front], arr[back]);
back--;
}
}
}
/* Driver program to test above function */
int main()
{
int A[] = {4, 5, 6, 10, 12, 12};
int sum = 16;
int arr_size = 6;
printPairs(A, arr_size, sum);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIHByaW50UGFpcnMoaW50IGFycltdLCBpbnQgYXJyX3NpemUsIGludCBzdW0pCnsKICAgIGludCBmcm9udCA9IDA7CiAgICBpbnQgYmFjayA9IGFycl9zaXplIC0gMTsKCiAgICB3aGlsZSAoZnJvbnQgPCBiYWNrKSB7CiAgICAgICAgaWYgKGFycltmcm9udF0gKyBhcnJbYmFja10gPCBzdW0pIHsKICAgICAgICAgICAgZnJvbnQrKzsKICAgICAgICB9IGVsc2UgaWYgKGFycltmcm9udF0gKyBhcnJbYmFja10gPiBzdW0pIHsKICAgICAgICAgICAgYmFjay0tOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHByaW50ZiAoIlBhaXIgd2l0aCBnaXZlbiBzdW0gJWQgaXMgKCVkLCAlZCkgXG4iLAogICAgICAgICAgICAgICAgICAgIHN1bSwgYXJyW2Zyb250XSwgYXJyW2JhY2tdKTsKICAgICAgICAgICAgYmFjay0tOwogICAgICAgIH0KICAgIH0KfQoKLyogRHJpdmVyIHByb2dyYW0gdG8gdGVzdCBhYm92ZSBmdW5jdGlvbiAqLwppbnQgbWFpbigpCnsKICAgIGludCBBW10gPSB7NCwgNSwgNiwgMTAsIDEyLCAxMn07CiAgICBpbnQgc3VtID0gMTY7CiAgICBpbnQgYXJyX3NpemUgPSA2OwoKICAgIHByaW50UGFpcnMoQSwgYXJyX3NpemUsIHN1bSk7CgogICAgcmV0dXJuIDA7Cn0K