Last active
December 1, 2019 01:53
-
-
Save sundeepblue/812184b5efae0ab70c88 to your computer and use it in GitHub Desktop.
find the minimal difference between two sorted arrays elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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