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:
arr1containsuniqueCnt1distinct positive integers, each of which is not divisible bydivisor1.arr2containsuniqueCnt2distinct positive integers, each of which is not divisible bydivisor2.- No integer is present in both
arr1andarr2.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code