fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n=5,target=-2;
  7.  
  8. vector<int> a= {0, -1, 2, -3, 1};
  9.  
  10.  
  11. sort(a.begin(),a.end());
  12. for(int i : a ){
  13. cout<<i<<"\n";
  14. }
  15.  
  16. int i=0,j=n-1,temp=0;
  17.  
  18. while(i<j){
  19. int sum=a[i]+a[j];
  20.  
  21. if(sum==target){
  22. cout<<"Sum found"<<a[i]<<","<<a[j];
  23. temp=1;
  24. break;
  25.  
  26.  
  27. }
  28. else if(sum >target){
  29. j--;
  30. }
  31. else{
  32. i++;
  33. }
  34.  
  35.  
  36.  
  37. }
  38. if(temp==0){
  39. cout<<"Sum not found";
  40. }
  41.  
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
-3
-1
0
1
2
Sum found-3,1