#include <iostream>
#include <algorithm>
using namespace std;
void findpair(int arr[], int len, int sum);
int main(void)
{
int len = 10;
int arr[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 3};
findpair(arr, len, 4);
}
void findpair(int arr[], int len, int sum)
{
std::sort(arr, arr+len);
int i = 0;
int j = len -1;
while( i < j){
while((arr[i] + arr[j]) <= sum && i < j)
{
if((arr[i] + arr[j]) == sum)
cout << "(" << arr[i] << "," << arr[j] << ")" << endl;
i++;
}
j--;
while((arr[i] + arr[j]) >= sum && i < j)
{
if((arr[i] + arr[j]) == sum)
cout << "(" << arr[i] << "," << arr[j] << ")" << endl;
j--;
}
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgZmluZHBhaXIoaW50IGFycltdLCBpbnQgbGVuLCBpbnQgc3VtKTsKCmludCBtYWluKHZvaWQpCnsKICAgIGludCBsZW4gPSAxMDsKICAgIGludCBhcnJbXSA9IHsxLCAxLCAxLCAyLCAyLCAyLCAzLCAzLCAzLCAzfTsKICAgIGZpbmRwYWlyKGFyciwgbGVuLCA0KTsKfQoKdm9pZCBmaW5kcGFpcihpbnQgYXJyW10sIGludCBsZW4sIGludCBzdW0pCnsKICAgIHN0ZDo6c29ydChhcnIsIGFycitsZW4pOwogICAgaW50IGkgPSAwOwogICAgaW50IGogPSBsZW4gLTE7CiAgICB3aGlsZSggaSA8IGopewogICAgICAgIHdoaWxlKChhcnJbaV0gKyBhcnJbal0pIDw9IHN1bSAmJiBpIDwgaikKICAgICAgICB7CiAgICAgICAgICAgIGlmKChhcnJbaV0gKyBhcnJbal0pID09IHN1bSkKICAgICAgICAgICAgICAgIGNvdXQgPDwgIigiIDw8IGFycltpXSA8PCAiLCIgPDwgYXJyW2pdIDw8ICIpIiA8PCBlbmRsOwogICAgICAgICAgICBpKys7CiAgICAgICAgfQogICAgICAgIGotLTsKICAgICAgICB3aGlsZSgoYXJyW2ldICsgYXJyW2pdKSA+PSBzdW0gJiYgaSA8IGopCiAgICAgICAgewogICAgICAgICAgICBpZigoYXJyW2ldICsgYXJyW2pdKSA9PSBzdW0pCiAgICAgICAgICAgICAgICBjb3V0IDw8ICIoIiA8PCBhcnJbaV0gPDwgIiwiIDw8IGFycltqXSA8PCAiKSIgPDwgZW5kbDsKICAgICAgICAgICAgai0tOwogICAgICAgIH0KICAgIH0KfQ==