Divisible Game Problem
Description
LeetCode Problem 3984.
You are given an integer array nums of length n.
Alice and Bob are playing a game. Alice chooses:
- An integer
ksuch thatk > 1. - Two integers
landrsuch that0 <= l <= r < n.
Initially, both Alice's and Bob's scores are 0.
For each index i in the range [l, r] (inclusive):
- If
nums[i]is divisible byk, Alice's score increases bynums[i]. - Otherwise, Bob's score increases by
nums[i].
The score difference is Alice's score minus Bob's score.
Alice wants to maximize the score difference. If there are multiple values of k that achieve the maximum score difference, she chooses the smallest such k.
Return the product of the maximum score difference and the chosen value of k. Since the result can be large, return it modulo 10^9 + 7.
Example 1:
Input: nums = [1,4,6,8]
Output: 36
Explanation:
- Alice can choose k = 2, l = 1, and r = 3.
- All values in nums[1..3] are divisible by 2, so Alice's score is 4 + 6 + 8 = 18, while Bob's score is 0.
- The score difference is 18, which is the maximum possible. Among all values of k that achieve this score difference, the smallest is 2.
- Therefore, the answer is 18 * 2 = 36.
Example 2:
Input: nums = [2,1,2]
Output: 6
Explanation:
- Alice can choose k = 2, l = 0, and r = 2.
- The values nums[0] and nums[2] are divisible by 2, so Alice's score is 2 + 2 = 4. The value nums[1] is not divisible by 2, so Bob's score is 1.
- The score difference is 4 - 1 = 3, which is the maximum possible. Among all values of k that achieve this score difference, the smallest is 2.
- Therefore, the answer is 3 * 2 = 6.
Example 3:
Input: nums = [1]
Output: 1000000005
Explanation:
- Alice must choose some k > 1. The smallest possible choice is k = 2.
- Since nums[0] is not divisible by 2, Alice's score is 0, while Bob's score is 1.
- The score difference is -1, which is the maximum possible.
- Therefore, the answer is -1 * 2 = -2. Modulo 10^9 + 7, this equals 1000000005.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 10^6
Solution
Approach: Group the multiples by divisor
For a fixed k, an element divisible by k adds +x to the difference and every other element adds -x. The best subarray for this k is therefore a maximum subarray sum where multiples count positively and the rest negatively.
Only values of k > 1 that divide at least one element can give a positive difference, and the divisors of a number up to 10^6 can be listed in O(sqrt(x)). For each such k, collect the indices of its multiples. A best subarray starts and ends on a multiple, so run Kadane's algorithm over these indices only, subtracting the sum of the skipped elements between two consecutive multiples (computed with prefix sums). The total number of indices over all k is the total number of divisors, which is small.
If no k has a multiple then the difference is -min(nums), and k = 2 always reaches at least that value, so k = 2 is always a candidate. Scan the candidates in increasing order, keep the first one with the largest difference, and return diff * k modulo 10^9 + 7, taking care that the difference can be negative.
Complexity: time O(n * sqrt(M)) for the divisors plus O(n * d) for the scans, where M is the largest value and d the maximum number of divisors, space O(n * d).
class Solution:
def divisibleGame(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
pre = [0] * (n + 1)
for i, x in enumerate(nums):
pre[i + 1] = pre[i] + x
# positions of the multiples of every k > 1 that divides some element
pos = {2: []}
for i, x in enumerate(nums):
d = 1
while d * d <= x:
if x % d == 0:
for k in {d, x // d}:
if k > 1:
pos.setdefault(k, []).append(i)
d += 1
best = None
best_k = 0
for k in sorted(pos):
idx = pos[k]
if not idx:
val = -min(nums)
else:
val = cur = 0
prev = -1
for p in idx:
if prev < 0:
cur = nums[p]
else:
gap = pre[p] - pre[prev + 1]
cur = nums[p] + max(0, cur - gap)
val = max(val, cur)
prev = p
if best is None or val > best:
best = val
best_k = k
return best * best_k % mod