Minimize the Maximum of Two Arrays - Problem

We have two arrays arr1 and arr2 which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:

  • arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1.
  • arr2 contains uniqueCnt2 distinct positive integers, each of which is not divisible by divisor2.
  • No integer is present in both arr1 and arr2.

Given divisor1, divisor2, uniqueCnt1, and uniqueCnt2, return the minimum possible maximum integer that can be present in either array.

Input & Output

Example 1 — Basic Case
$ Input: divisor1 = 2, divisor2 = 3, uniqueCnt1 = 1, uniqueCnt2 = 1
Output: 2
💡 Note: We can put 1 in arr1 (not divisible by 2) and 2 in arr2 (not divisible by 3). The maximum element is 2.
Example 2 — Larger Requirements
$ Input: divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
Output: 3
💡 Note: For maxVal=3: Numbers not divisible by 3: {1,2}. Numbers not divisible by 5: {1,2,3}. arr1 needs 2 elements, takes {1,2}. arr2 needs 1 element from remaining {3}. Maximum element is 3.
Example 3 — Same Divisors
$ Input: divisor1 = 2, divisor2 = 2, uniqueCnt1 = 1, uniqueCnt2 = 1
Output: 2
💡 Note: Both arrays need elements not divisible by 2. Available: {1,3,5,...}. For maxVal=2, only {1} is available, but we need 2 elements total. For maxVal=3, available: {1,3}. arr1 takes 1, arr2 takes 3. But since we're minimizing maximum and both need elements from same set, minimum maximum is 2 if we can use {1} for one array, but we need 2 distinct elements, so minimum maximum is actually 3.

Constraints

  • 2 ≤ divisor1, divisor2 ≤ 105
  • 1 ≤ uniqueCnt1, uniqueCnt2 ≤ 109

Visualization

Tap to expand
Minimize the Maximum of Two Arrays INPUT arr1: needs uniqueCnt1 numbers Not divisible by d1 arr2: needs uniqueCnt2 numbers Not divisible by d2 No overlap allowed! Given Values: divisor1 = 2 divisor2 = 3 uniqueCnt1=1 uniqueCnt2=2 Numbers 1,2,3,4,5... 1 2 3 4 5 ALGORITHM STEPS 1 Binary Search Setup Search range: [1, 2*(cnt1+cnt2)] 2 Count Valid Numbers For mid M, count nums not divisible by d1, d2, both 3 Check Feasibility Can we fill both arrays with max value = M? 4 Find Minimum Max Binary search for smallest feasible M Key Formulas: cnt_not_d1 = M - M/d1 cnt_not_d2 = M - M/d2 cnt_both = M - M/d1 - M/d2 + M/lcm(d1,d2) FINAL RESULT Optimal Assignment: arr1 (not div by 2): 1 arr2 (not div by 3): 2 Maximum: 3 Verification: - 1 is not divisible by 2 [OK] - 2 is not divisible by 3 [OK] - No overlap, max = 3 [OK] Key Insight: Binary search on the answer! For a candidate max M, use inclusion-exclusion to count how many numbers in [1,M] can go to arr1 only, arr2 only, or both. Check if we can satisfy uniqueCnt1 and uniqueCnt2 requirements without overlap. LCM handles both divisors. TutorialsPoint - Minimize the Maximum of Two Arrays | Optimal Solution
Asked in
Google 25 Microsoft 18 Amazon 15
12.5K Views
Medium Frequency
~25 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen