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,4,5,7};
  9. vector<int>arr2={10,20,30,40};
  10. int x=32;
  11. int a,b;
  12. int closest_sum=INT_MAX;
  13. long long answer=-10000000000000;
  14. for(int i=0;i<arr1.size();i++)
  15. {
  16. int sum=0;
  17. for(int j=0;j<arr2.size();j++)
  18. {
  19. sum=arr1[i]+arr2[j];
  20. if(sum<=x)
  21. {
  22. // int y = abs(x-sum);
  23. // //closest_sum=min(y,closest_sum);
  24. // if(closest_sum>y)
  25. // {
  26. // closest_sum=y;
  27. // a=arr1[i];
  28. // b=arr2[j];
  29. // }
  30. if(answer<sum)
  31. {
  32. answer=sum;
  33. a=arr1[i];
  34. b=arr2[j];
  35. }
  36.  
  37. }
  38. }
  39. }
  40. cout<<a<<" "<<b;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
1 30