#include <stdio.h>
void printPairs(int arr[], int arr_size, int sum)
{
int i = 0;
int temp = 0;
int bitmap = 0; // all bits are 0
int bitmask = 0;
for(i = 0; i < arr_size; i++)
{
temp = sum - arr[i];
bitmask = 0;
bitmask = 1 << temp;
if(temp >= 0 && (bitmap & bitmask))
{
printf("Pair with given sum %d is (%d, %d) \n", sum
, arr
[i
], temp
); }
bitmap |= 1 << arr[i];
}
}
/* Driver program to test above function */
int main()
{
int A[] = {12, 4, 13, 6, 10, 3};
int sum = 16;
int arr_size = 6;
printPairs(A, arr_size, sum);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIHByaW50UGFpcnMoaW50IGFycltdLCBpbnQgYXJyX3NpemUsIGludCBzdW0pCnsKICAgIGludCBpID0gMDsKICAgIGludCB0ZW1wID0gMDsKICAgIGludCBiaXRtYXAgPSAwOyAvLyBhbGwgYml0cyBhcmUgMAogICAgaW50IGJpdG1hc2sgPSAwOwoKICAgIGZvcihpID0gMDsgaSA8IGFycl9zaXplOyBpKyspCiAgICB7CiAgICAgICAgdGVtcCA9IHN1bSAtIGFycltpXTsKCiAgICAgICAgYml0bWFzayA9IDA7CiAgICAgICAgYml0bWFzayA9IDEgPDwgdGVtcDsKCgogICAgICAgIGlmKHRlbXAgPj0gMCAmJiAoYml0bWFwICYgYml0bWFzaykpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIlBhaXIgd2l0aCBnaXZlbiBzdW0gJWQgaXMgKCVkLCAlZCkgXG4iLCBzdW0sIGFycltpXSwgdGVtcCk7CiAgICAgICAgfQogICAgICAgIGJpdG1hcCB8PSAxIDw8IGFycltpXTsKICAgIH0KfQoKCi8qIERyaXZlciBwcm9ncmFtIHRvIHRlc3QgYWJvdmUgZnVuY3Rpb24gKi8KaW50IG1haW4oKQp7CiAgICBpbnQgQVtdID0gezEyLCA0LCAxMywgNiwgMTAsIDN9OwogICAgaW50IHN1bSA9IDE2OwogICAgaW50IGFycl9zaXplID0gNjsKCiAgICBwcmludFBhaXJzKEEsIGFycl9zaXplLCBzdW0pOwoKICAgIHJldHVybiAwOwp9Cg==