Given three sorted arrays A, B, and C of not necessarily same sizes. Calculate the minimum absolute difference between the maximum and minimum number of any triplet A[i], B[j], C[k] such that they belong to arrays A, B and C respectively, i.e., minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k]))
Input : A : [ 1, 4, 5, 8, 10 ] B : [ 6, 9, 15 ] C : [ 2, 3, 6, 6 ] Output : 1 Explanation: When we select A[i] = 5 B[j] = 6, C[k] = 6, we get the minimum difference as max(A[i], B[j], C[k]) - min(A[i], B[j], C[k])) = |6-5| = 1 Input : A = [ 5, 8, 10, 15 ] B = [ 6, 9, 15, 78, 89 ] C = [ 2, 3, 6, 6, 8, 8, 10 ] Output : 1 Explanation: When we select A[i] = 10 b[j] = 9, C[k] = 10.
Start with the largest elements in each of the arrays A, B & C. Maintain a variable to update the answer during each of the steps to be followed.
In every step, the only possible way to decrease the difference is to decrease the maximum element out of the three elements.
So traverse to the next largest element in the array containing the maximum element for this step and update the answer variable.
Repeat this step until the array containing the maximum element ends.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Minimize Steps required to obtain Sorted Order of an Array
- Minimize the sum after choosing elements from the given three arrays
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Minimize the sum of product of two arrays with permutations allowed
- Merge 3 Sorted Arrays
- Merge two sorted arrays
- K-th Element of Two Sorted Arrays
- Merge k sorted arrays | Set 1
- Find m-th smallest value in k sorted arrays
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
- Union and Intersection of two sorted arrays
- Median of two sorted arrays of different sizes
- Median of two sorted arrays of same size
- Find three closest elements from given three sorted arrays
- Merging two unsorted arrays in sorted order
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Find common elements in three sorted arrays
- Find the closest pair from two sorted arrays
- Median of two sorted arrays of different sizes | Set 1 (Linear)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.