fork download
  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main() {
  5.  
  6. int n,k;
  7. cin>>n;
  8. int *arr = new int[n];
  9.  
  10. for(int i=0;i<n;i++)
  11. {
  12. cin>>arr[i];
  13. }
  14. cin>>k;
  15. sort(arr,arr+n);
  16. for(int i=0;i<n;i++)
  17. {
  18. unordered_set<int> st;
  19. int sum = k - arr[i];
  20. for(int j=i+1;j<n;j++)
  21. {
  22. if(st.find(sum-arr[j])!=st.end())
  23. {
  24. cout<<arr[i]<<","<<" "<<sum-arr[j]<<" "<<"and"<<" "<<arr[j]<<endl;
  25. }
  26.  
  27. st.insert(arr[j]);
  28. }
  29. }
  30.  
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 15240KB
stdin
9
5
7
9
1
2
4
6
8
3
10
stdout
1, 4 and 5
1, 3 and 6
1, 2 and 7
2, 3 and 5