You are given an integer array nums of length n.
Alice and Bob are playing a game. Alice chooses:
k such that k > 1.l and r such that 0 <= l <= r < n.Initially, both Alice's and Bob's scores are 0.
For each index i in the range [l, r] (inclusive):
nums[i] is divisible by k, Alice's score increases by nums[i].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 109 + 7.
Example 1:
Input: nums = [1,4,6,8]
Output: 36
Explanation:
k = 2, l = 1, and r = 3.nums[1..3] are divisible by 2, so Alice's score is 4 + 6 + 8 = 18, while Bob's score is 0.k that achieve this score difference, the smallest is 2.18 * 2 = 36.Example 2:
Input: nums = [2,1,2]
Output: 6
Explanation:
k = 2, l = 0, and r = 2.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.4 - 1 = 3, which is the maximum possible. Among all values of k that achieve this score difference, the smallest is 2.3 * 2 = 6.Example 3:
Input: nums = [1]
Output: 1000000005
Explanation:
k > 1. The smallest possible choice is k = 2.nums[0] is not divisible by 2, Alice's score is 0, while Bob's score is 1.-1 * 2 = -2. Modulo 109 + 7, this equals 1000000005.Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 106The game requires selecting an integer divisor greater than one and a contiguous subarray to maximize the difference between Alice's and Bob's scores. For any chosen divisor, each element in the subarray contributes positively if it is divisible by the divisor and negatively otherwise, which reduces to finding the maximum subarray sum using Kadane's algorithm. Because any composite divisor is a multiple of a prime and divides at most the same elements, we only need to test prime factors of the numbers in the array along with the minimum valid divisor 2.
Here's how the algorithm would work step-by-step:
def divisible_game(nums):
# Find all unique prime factors and candidate divisors greater than 1
candidate_divisors = set([2])
for number in nums:
temp_val = number
divisor = 2
while divisor * divisor <= temp_val:
if temp_val % divisor == 0:
candidate_divisors.add(divisor)
while temp_val % divisor == 0:
temp_val //= divisor
divisor += 1
if temp_val > 1:
candidate_divisors.add(temp_val)
best_difference = -float('inf')
chosen_divisor = 2
modulus = 10**9 + 7
# Evaluate the maximum subarray score difference for each candidate divisor
for divisor in sorted(candidate_divisors):
current_sum = 0
max_diff_for_divisor = -float('inf')
for number in nums:
score_contribution = number if number % divisor == 0 else -number
current_sum = max(score_contribution, current_sum + score_contribution)
if current_sum > max_diff_for_divisor:
max_diff_for_divisor = current_sum
# Update the overall best score difference and the chosen divisor
if max_diff_for_divisor > best_difference:
best_difference = max_diff_for_divisor
chosen_divisor = divisor
result = (best_difference * chosen_divisor) % modulus
return result
For a fixed divisor k, each element in the chosen subarray contributes positively if it is divisible by k and negatively otherwise. Finding the optimal subarray for a fixed k is equivalent to the maximum subarray sum problem, which can be solved using Kadane's algorithm. Because any element divisible by a composite number is also divisible by its prime factors, testing prime factors of elements alongside a default candidate suffices.
Here's how the algorithm would work step-by-step:
def divisible_game(nums: list[int]) -> int:
modulo = 10**9 + 7
distinct_factors = set()
# Find all prime factors and divisors greater than 1 from the array elements
for value in nums:
temp_val = value
divisor = 2
while divisor * divisor <= temp_val:
if temp_val % divisor == 0:
distinct_factors.add(divisor)
while temp_val % divisor == 0:
temp_val //= divisor
divisor += 1
if temp_val > 1:
distinct_factors.add(temp_val)
# Always include 2 as a baseline candidate
distinct_factors.add(2)
candidate_list = sorted(distinct_factors)
best_diff = -float('inf')
best_k = 2
# Evaluate the maximum subarray sum for each candidate divisor
for candidate in candidate_list:
current_max = -float('inf')
running_sum = 0
for num in nums:
gain = num if num % candidate == 0 else -num
running_sum = max(gain, running_sum + gain)
if running_sum > current_max:
current_max = running_sum
# Update the best score difference and the minimal candidate
if current_max > best_diff:
best_diff = current_max
best_k = candidate
return (best_diff * best_k) % modulo
| Case | How to Handle |
|---|---|
| Empty or single-element input arrays | Return false immediately since no valid moves can be made by the first player. |
| Arrays containing duplicate numbers | Treat each element distinctly by index or frequency to ensure proper state transitions during the game. |
| Inputs with zeros or negative numbers | Filter out or safely handle zeros to avoid division by zero errors when checking divisibility rules. |
| Inputs with all pairwise coprime numbers | Recognize that no moves are possible from the start, resulting in an immediate loss for the first player. |
| Maximum-sized input arrays leading to deep recursion or state explosion | Use memoization or bottom-up dynamic programming to avoid redundant calculations and stack overflows. |
| Scores forming a strict chain of divisibility | Simulate the deterministic elimination sequence to correctly determine the parity of the winning player. |