title: swap pair to make sum equal
body: <p>Question: Given two arrays of integers A[] and B[] of size N and M, the task is to check if a pair of values (one value from each array) exists such that swapping the elements of the pair will make the sum of two arrays equal.</p>
  <p>My approach:</p>
  <ol>
  <li>find sum of both arrays.</li>
  <li>Identify array with larger sum(denote with A[]).</li>
  <li>Sort A.</li>
  <li>For all values in B binary search (sum(A)-sum(B)/2 + B[i]) in A, if found return true.</li>
  <li>Return false.</li>
  </ol>
  <p>code:</p>
  <pre><code>int sum(int a[], int n){
      int s=0;
      for(int i=0; i&lt;n; i++){
          s+= a[i];
      }
      return s;
  }
  
  int findSwapValues(int A[], int n, int B[], int m)
  {
      // Your code goes here
      int a = sum(A, n);
      int b = sum(B,m);
      int t;
      int *temp;
      
      if(a&lt;b){
          temp  = A; 
          A = B;
          B = temp;
          t  = n;
          n = m;
          m = t;
          
          t = a;
          a = b;
          b = t;
      }
      
      sort(A, A+n);
      for(int i=0; i&lt;m; i++){
          if(binary_search(A,A+n,(a-b)/2+B[i])){
              return 1;
          }
      }
      return -1;
  }
  </code></pre>
  <p>Doubt: My algorithm is failing for some test cases(not TLE). As the test cases are very large, it's difficult to reason out the problem in the algorithm. I searched online and understood other approaches. My only curiosity is why its incorrect?</p>