fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. #include<vector>
  5. int main() {
  6. // your code goes here
  7.  
  8. vector<int>arr1={1,2,3,4,5};
  9. vector<int>arr2={800,1000,1500,1550,1800};
  10. int target=1100;
  11. int a,b,sum;
  12. int closest_sum=INT_MAX;
  13. long long answer=-10000000000000;
  14. int right=arr2.size()-1;
  15. int left;
  16. while(left<arr1.size() && right>=0) // or while(left<arr1.size() && right>=0)
  17. {
  18. sum=arr1[left]+arr2[right];
  19. if(sum>target)
  20. {
  21. right--;
  22. }
  23. else if(sum<=target)
  24. {
  25. if(sum>answer)
  26. {
  27. answer=sum;
  28. a=arr1[left];
  29. b=arr2[right];
  30. }
  31. left++;
  32. }
  33. }
  34.  
  35. cout<<a<<" "<<b;
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
5 1000