fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. namespace mj {
  6. void search(int *A, int *B, int M, int N, int answer)
  7. {
  8. if(M > N) return search(B, A, N, M, answer);
  9.  
  10. for(int i = 0; i < M; ++i) {
  11. int rem = answer - A[i];
  12. if( binary_search(B, B+N, rem) ) {
  13. cout << answer << " found" << endl;
  14. return;
  15. }
  16. }
  17. cout << answer << " not found" << endl;
  18. }
  19. }
  20.  
  21. int main() {
  22. int A[] = {1,2,3,4,5};
  23. int B[] = {6, 7, 8, 9, 10}; // Assuming both are sorted
  24. const int M = sizeof A / sizeof A[0];
  25. const int N = sizeof B / sizeof B[0];
  26.  
  27. mj::search(A, B, M, N, 13);
  28. mj::search(A, B, M, N, 16);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
13 found
16 not found