Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sundeepblue/812184b5efae0ab70c88 to your computer and use it in GitHub Desktop.
Save sundeepblue/812184b5efae0ab70c88 to your computer and use it in GitHub Desktop.
find the minimal difference between two sorted arrays elements
int find_min_diff(vector<int>& A, vector<int>& B) {
if(A.empty() || B.empty()) return -1;
int m = A.size(), n = B.size();
int i = 0, j = 0;
int min_diff = INT_MAX;
while(i < m && j < n) {
int diff = abs(A[i] - B[j]);
min_diff = min(min_diff, diff);
if(min_diff == 0) return 0;
if(A[i] > B[j]) j++;
else i++;
}
return min_diff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment