Maximize Subarray GCD Score - Problem

You're given an array of positive integers nums and an integer k representing the maximum number of operations you can perform. Your goal is to maximize the score of any contiguous subarray after applying strategic modifications.

Operations Available:

  • You can perform at most k operations
  • Each operation doubles the value of any element in the array
  • Each element can be doubled at most once

Subarray Score Calculation:

The score of a contiguous subarray equals: Length ร— GCD(all elements in subarray)

Where GCD is the Greatest Common Divisor - the largest integer that evenly divides all elements in the subarray.

Your Task: Return the maximum possible score achievable by selecting any contiguous subarray from the strategically modified array.

Example: For nums = [12, 18, 6], k = 1, if we double the element 6 to get [12, 18, 12], the subarray [12, 18, 12] has GCD = 6 and length = 3, giving score = 18.

Input & Output

example_1.py โ€” Basic case
$ Input: [12, 18, 6] k = 1
โ€บ Output: 24
๐Ÿ’ก Note: Double the element 12 to get [24, 18, 6]. The subarray [24] has GCD=24 and length=1, giving score=24. This is better than keeping the original array where [12, 18, 6] has GCD=6 and score=18.
example_2.py โ€” Multiple operations
$ Input: [4, 4, 4] k = 2
โ€บ Output: 24
๐Ÿ’ก Note: Double two elements to get [8, 8, 4]. The subarray [8, 8] has GCD=8 and length=2, giving score=16. Or double to get [4, 8, 8] where [8, 8] gives score=16. Actually, [8, 4] has GCD=4, so [8, 8, 4] has GCD=4 and score=12. The best is doubling to get [8, 8, 4] and taking [8, 8] for score=16.
example_3.py โ€” No operations needed
$ Input: [10, 20, 30] k = 0
โ€บ Output: 30
๐Ÿ’ก Note: Cannot perform any operations. The subarray [30] has GCD=30 and length=1, giving the maximum score=30. The full array [10, 20, 30] has GCD=10 and length=3, giving score=30 as well.

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 106
  • 0 โ‰ค k โ‰ค nums.length
  • Each element can be doubled at most once
  • All array elements are positive integers

Visualization

Tap to expand
๐ŸŽผ Orchestra Harmony Optimization๐ŸŽต Musicians: [12, 18, 6] with 1 Volume Boost Available12Violin18Cello6Flute๐ŸŽฏ Harmony AnalysisHarmony Level 6All musicians can play at this levelโ€ข Violin(12) โœ“ - natural harmonyโ€ข Cello(18) โœ“ - natural harmonyโ€ข Flute(6) โœ“ - natural harmonyScore: 3 musicians ร— 6 = 18Harmony Level 24Use volume boost on Violinโ€ข Violin(12โ†’24) โœ“ - with boostโ€ข Cello(18) โœ— - can't reach 24โ€ข Flute(6) โœ— - can't reach 24Score: 1 musician ร— 24 = 24๐Ÿ† Optimal Strategy Found!Best approach: Use the volume boost on Violin (12โ†’24)This creates a solo performance with harmony level 24๐ŸŽต Maximum Score: 1 ร— 24 = 24 points๐Ÿ’ก Key Insight:Sometimes a smallerensemble with higherharmony beats a largergroup with lowerharmony level!
Understanding the Visualization
1
Identify Harmony Levels
List all possible GCD values that could be achieved
2
Group Musicians
For each harmony level, find which musicians can contribute
3
Optimize Volume
Decide how to best use limited volume boosts (k operations)
4
Find Best Section
Locate the longest consecutive group achieving target harmony
Key Takeaway
๐ŸŽฏ Key Insight: By systematically exploring all possible harmony levels (GCD values) instead of all possible volume boost combinations, we can efficiently find the optimal strategy that maximizes the total score.
Asked in
Google 45 Microsoft 38 Meta 22 Amazon 15
27.5K Views
Medium-High Frequency
~35 min Avg. Time
892 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