Minimize the Maximum of Two Arrays - Problem

Imagine you're a data architect tasked with constructing two special arrays with very specific requirements. You need to build arr1 and arr2 from scratch, starting with empty arrays, by carefully selecting positive integers.

Your mission: Fill these arrays while satisfying all these constraints:

  • arr1 must contain exactly uniqueCnt1 distinct positive integers, where none are divisible by divisor1
  • arr2 must contain exactly uniqueCnt2 distinct positive integers, where none are divisible by divisor2
  • No number can appear in both arrays - they must be completely disjoint

Your goal is to minimize the largest number that appears in either array. Think of it as trying to keep both arrays as "small" as possible while meeting all requirements.

Example: If divisor1=2, divisor2=3, uniqueCnt1=1, uniqueCnt2=1, you could put 1 in arr1 (not divisible by 2) and 2 in arr2 (not divisible by 3). The maximum would be 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: divisor1 = 2, divisor2 = 3, uniqueCnt1 = 1, uniqueCnt2 = 1
โ€บ Output: 2
๐Ÿ’ก Note: We need 1 number for arr1 (not divisible by 2) and 1 number for arr2 (not divisible by 3). We can use arr1 = [1] and arr2 = [2]. The maximum is 2.
example_2.py โ€” Larger Counts
$ Input: divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
โ€บ Output: 3
๐Ÿ’ก Note: We need 2 numbers for arr1 (not divisible by 3) and 1 for arr2 (not divisible by 5). We can use arr1 = [1, 2] and arr2 = [3]. Maximum is 3.
example_3.py โ€” Edge Case with Same Divisors
$ Input: divisor1 = 2, divisor2 = 2, uniqueCnt1 = 1, uniqueCnt2 = 1
โ€บ Output: 3
๐Ÿ’ก Note: Both arrays need numbers not divisible by 2. We can use arr1 = [1] and arr2 = [3] since no number can be in both arrays. Maximum is 3.

Constraints

  • 2 โ‰ค divisor1, divisor2 โ‰ค 105
  • 1 โ‰ค uniqueCnt1, uniqueCnt2 โ‰ค 109
  • No integer appears in both arrays
  • All numbers must be positive integers

Visualization

Tap to expand
Number Distribution VisualizationOnly for arr1Not รท divisor1But รท divisor2FlexibleNot รท divisor1Not รท divisor2Only for arr2Not รท divisor2But รท divisor1arr1arr2Binary search finds minimum maximum efficientlyKey: Use inclusion-exclusion to count valid numbers in O(1)
Understanding the Visualization
1
Categorize Numbers
Group numbers by what arrays they can join based on divisibility
2
Count Available Numbers
Use inclusion-exclusion to count valid numbers up to any maximum
3
Binary Search Range
Search for minimum maximum that allows valid distribution
4
Greedy Allocation
Use flexible numbers first, then array-specific numbers
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on the answer combined with mathematical counting makes this problem tractable even for large inputs
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
27.4K Views
Medium Frequency
~25 min Avg. Time
890 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