#include <stdio.h>
#define MAX 100000
void printPairs(int arr[], int arr_size, int sum)
{
int i = 0;
int temp = 0;
int binMap[MAX] = {0}; /*initialize hash map as 0*/
for(i = 0; i < arr_size; i++)
{
temp = sum - arr[i];
if(temp >= 0 && binMap[temp] == 1)
{
printf("Pair with given sum %d is (%d, %d) \n", sum
, arr
[i
], temp
); }
binMap[arr[i]] = 1;
}
}
/* Driver program to test above function */
int main()
{
int A[] = {12, 4, 45, 6, 10, 12};
int sum = 16;
int arr_size = 6;
printPairs(A, arr_size, sum);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNkZWZpbmUgTUFYIDEwMDAwMAoKdm9pZCBwcmludFBhaXJzKGludCBhcnJbXSwgaW50IGFycl9zaXplLCBpbnQgc3VtKQp7CiAgICBpbnQgaSA9IDA7CiAgICBpbnQgdGVtcCA9IDA7CiAgICBpbnQgYmluTWFwW01BWF0gPSB7MH07IC8qaW5pdGlhbGl6ZSBoYXNoIG1hcCBhcyAwKi8KCiAgICBmb3IoaSA9IDA7IGkgPCBhcnJfc2l6ZTsgaSsrKQogICAgewoJdGVtcCA9IHN1bSAtIGFycltpXTsKCWlmKHRlbXAgPj0gMCAmJiBiaW5NYXBbdGVtcF0gPT0gMSkKCXsKCSAgICBwcmludGYoIlBhaXIgd2l0aCBnaXZlbiBzdW0gJWQgaXMgKCVkLCAlZCkgXG4iLCBzdW0sIGFycltpXSwgdGVtcCk7Cgl9CgliaW5NYXBbYXJyW2ldXSA9IDE7CiAgICB9Cn0KCi8qIERyaXZlciBwcm9ncmFtIHRvIHRlc3QgYWJvdmUgZnVuY3Rpb24gKi8KaW50IG1haW4oKQp7CiAgICBpbnQgQVtdID0gezEyLCA0LCA0NSwgNiwgMTAsIDEyfTsKICAgIGludCBzdW0gPSAxNjsKICAgIGludCBhcnJfc2l6ZSA9IDY7CgogICAgcHJpbnRQYWlycyhBLCBhcnJfc2l6ZSwgc3VtKTsKCiAgICByZXR1cm4gMDsKfQo=