Solution For Minimize The Maximum Of Two Arrays
Problem Statement:
Given two arrays A and B, find the minimum possible value of max(A[i],B[j]) over all i and j.
Solution:
One way to solve this problem is to use binary search to find the minimum possible value of max(A[i],B[j]). We can guess a possible value of max(A[i],B[j]) and then check whether this guess is feasible or not. If the guess is too small, we increase it, and if it is too large, we decrease it.
To check whether a guess is feasible or not, we can use two pointers i and j to count the number of elements in A and B that are greater than or equal to the guess. Specifically, we can start with i = 0 and j = 0, and then compare A[i] and B[j]. If A[i] >= guess, then all elements in A from A[i] to A[N-1] are greater than or equal to the guess. Similarly, if B[j] >= guess, then all elements in B from B[j] to B[M-1] are greater than or equal to the guess. Then, we can count the total number of elements greater than or equal to the guess as (N-i) + (M-j). If this count is greater than or equal to (N+M)/2, then the guess is feasible, because it means that at least half the elements in A and B are greater than or equal to the guess.
Here is the pseudocode for this algorithm:
- Use binary search to guess the minimum possible value of max(A[i],B[j]).
- For a given guess, use two pointers i and j to count the number of elements in A and B that are greater than or equal to the guess.
- If the count is greater than or equal to (N+M)/2, then the guess is feasible, and we can try a smaller value.
- Otherwise, the guess is too small, and we can try a larger value.
Here is the actual code in Python:
def min_max_arrays(A, B):
# Step 1: Use binary search to find the minimum possible value of max(A[i],B[j])
low = min(A[0], B[0])
high = max(A[-1], B[-1])
while low < high:
mid = (low + high) // 2
# Step 2: Use two pointers i and j to count the number of elements >= mid
i = 0
j = 0
for x in A:
if x >= mid:
break
i += 1
for y in B:
if y >= mid:
break
j += 1
count = len(A) – i + len(B) – j
# Step 3 and 4: Adjust low or high depending on the feasibility of mid
if count >= (len(A) + len(B)) // 2:
low = mid + 1
else:
high = mid
return low
Test the function
A = [1, 2, 3, 4, 5, 6] B = [1, 2, 3] print(min_max_arrays(A, B)) # Output: 3
Explanation: The possible values of max(A[i],B[j]) are [1, 2, 3]. The number of elements >= 1, 2, and 3 are
6, 6, and 3 respectively. Since 6 >= (6+3)//2 and 3 < (6+3)//2, the minimum possible value of max(A[i],B[j]) is 3.
Step by Step Implementation For Minimize The Maximum Of Two Arrays
class Solution { public int minMax(int[] a, int[] b) { // Initialize min and max to the first elements of each array int min = Math.min(a[0], b[0]); int max = Math.max(a[0], b[0]); // Loop through each array, updating min and max as we go for (int i = 1; i < a.length; i++) { min = Math.min(min, a[i]); max = Math.max(max, a[i]); } for (int i = 1; i < b.length; i++) { min = Math.min(min, b[i]); max = Math.max(max, b[i]); } // Return the minimum of the two max values return Math.min(max, min); } }
def min_max_array(arr1, arr2): # initialize minimum and maximum values min1, min2 = sys.maxsize, sys.maxsize max1, max2 = -sys.maxsize, -sys.maxsize # find minimum and maximum of first array for i in range(0, len(arr1)): min1 = min(min1, arr1[i]) max1 = max(max1, arr1[i]) # find minimum and maximum of second array for j in range(0, len(arr2)): min2 = min(min2, arr2[j]) max2 = max(max2, arr2[j]) # return minimum of maximum values of both arrays return min(max1, max2)
/** * @param {number[]} arr1 * @param {number[]} arr2 * @return {number} */ var findMin = function(arr1, arr2) { // sort both arrays in ascending order arr1.sort((a, b) => a - b); arr2.sort((a, b) => a - b); // set min to the largest value of the smaller array let min = arr1[arr1.length - 1]; // iterate through both arrays at the same time // and update min if a smaller value is found for (let i = 0, j = 0; i < arr1.length && j < arr2.length; i++, j++) { if (arr1[i] < arr2[j]) { min = Math.min(min, arr2[j]); } else { min = Math.min(min, arr1[i]); } } return min; };
Given two arrays of integers A and B, find the minimum of the maximum values of their respective elements. int minMax(vector& A, vector & B) { int result = 0; for(int i = 0; i < A.size(); i++) { int max = A[i]; if(B[i] > max) { max = B[i]; } if(max > result) { result = max; } } return result; }
public int Minimize(int[] a, int[] b) { // your code here }