Advertisement

Minimize the Maximum of Two Arrays - LeetCode 2513 Solution

Minimize the Maximum of Two Arrays - Complete Solution Guide

Minimize the Maximum of Two Arrays is LeetCode problem 2513, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 t

Detailed Explanation

The problem requires us to find the smallest possible maximum integer that can be present in either of two arrays, `arr1` and `arr2`. These arrays must satisfy the following conditions: `arr1` must contain `uniqueCnt1` distinct positive integers not divisible by `divisor1`, `arr2` must contain `uniqueCnt2` distinct positive integers not divisible by `divisor2`, and no integer can be present in both `arr1` and `arr2`. Essentially, we need to distribute integers such that we minimize the largest number used, while respecting the divisibility rules and uniqueness requirements.

Solution Approach

The solution uses binary search to find the minimum possible maximum integer. For a given mid value (representing a potential maximum integer), we determine the number of available slots for `arr1` and `arr2` by calculating the count of numbers up to `mid` that are not divisible by `divisor1` and `divisor2` respectively. We use the inclusion-exclusion principle to handle numbers divisible by both divisors using their least common multiple (LCM). Based on the available slots and the required counts (`uniqueCnt1` and `uniqueCnt2`), we check if the `mid` value is feasible. If it is, we try a smaller `mid` value; otherwise, we increase `mid`.

Step-by-Step Algorithm

  1. Step 1: Initialize `low` to 1 and `high` to a large value (e.g., 4 * 10^9) to define the search space for the minimum maximum integer.
  2. Step 2: Calculate the Least Common Multiple (LCM) of `divisor1` and `divisor2` using the formula: `LCM(a, b) = (a * b) / GCD(a, b)`. The GCD (Greatest Common Divisor) can be found using the Euclidean algorithm.
  3. Step 3: Perform binary search within the range `[low, high]`. In each iteration, calculate the `mid` value: `mid = low + (high - low) // 2`.
  4. Step 4: Calculate the number of slots available for `arr1` only: `arr1_only_slots = mid // divisor2 - mid // lcm`. This represents numbers divisible by `divisor2` but not `divisor1`.
  5. Step 5: Calculate the number of slots available for `arr2` only: `arr2_only_slots = mid // divisor1 - mid // lcm`. This represents numbers divisible by `divisor1` but not `divisor2`.
  6. Step 6: Calculate the number of common slots (numbers that can be placed in either `arr1` or `arr2`): `common_slots = mid - (mid // divisor1 + mid // divisor2 - mid // lcm)`. This represents numbers not divisible by either `divisor1` or `divisor2`.
  7. Step 7: Calculate the remaining counts `needed1` and `needed2`. `needed1` is the count left for arr1 after utilizing arr1_only_slots, and the same for `needed2` and arr2.
  8. Step 8: Check if the current `mid` is a feasible solution. If `common_slots >= needed1 + needed2`, it means we can accommodate all the required numbers within the given `mid`. Update `ans = mid` and narrow the search space by setting `high = mid - 1`.
  9. Step 9: If the current `mid` is not feasible, it means we need a larger `mid`. Update `low = mid + 1` to search in the upper half.
  10. Step 10: Repeat steps 3-9 until `low > high`. The final `ans` value will be the minimum possible maximum integer.
  11. Step 11: Cast ans to an integer and return it.

Key Insights

  • Insight 1: Binary search is applicable because we are looking for the minimum possible maximum value from a range. We can check if a given maximum value is feasible or not.
  • Insight 2: The core idea is to determine how many numbers are available for each array up to a given `mid` value (the potential maximum). This involves calculating the number of integers not divisible by `divisor1`, `divisor2`, or both.
  • Insight 3: We need to handle the overlap (numbers divisible by both `divisor1` and `divisor2`) correctly using the Least Common Multiple (LCM) to avoid double-counting.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(1)

Topics

This problem involves: Math, Binary Search, Number Theory.

Companies

Asked at: Goldman Sachs.