|
| 1 | +# [2941. Maximum GCD-Sum of a Subarray](https://leetcode.cn/problems/maximum-gcd-sum-of-a-subarray) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given an array of integers <code>nums</code> and an integer <code>k</code>.</p> |
| 10 | + |
| 11 | +<p>The <strong>gcd-sum</strong> of an array <code>a</code> is calculated as follows:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>Let <code>s</code> be the sum of all the elements of <code>a</code>.</li> |
| 15 | + <li>Let <code>g</code> be the <strong>greatest common divisor</strong> of all the elements of <code>a</code>.</li> |
| 16 | + <li>The gcd-sum of <code>a</code> is equal to <code>s * g</code>.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p>Return <em>the <strong>maximum gcd-sum</strong> of a subarray of</em> <code>nums</code> <em>with at least</em> <code>k</code> <em>elements.</em></p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> nums = [2,1,4,4,4,2], k = 2 |
| 26 | +<strong>Output:</strong> 48 |
| 27 | +<strong>Explanation:</strong> We take the subarray [4,4,4], the gcd-sum of this array is 4 * (4 + 4 + 4) = 48. |
| 28 | +It can be shown that we can not select any other subarray with a gcd-sum greater than 48.</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> nums = [7,3,9,4], k = 1 |
| 34 | +<strong>Output:</strong> 81 |
| 35 | +<strong>Explanation:</strong> We take the subarray [9], the gcd-sum of this array is 9 * 9 = 81. |
| 36 | +It can be shown that we can not select any other subarray with a gcd-sum greater than 81.</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>n == nums.length</code></li> |
| 43 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 45 | + <li><code>1 <= k <= n</code></li> |
| 46 | +</ul> |
| 47 | + |
| 48 | +## 解法 |
| 49 | + |
| 50 | +<!-- 这里可写通用的实现逻辑 --> |
| 51 | + |
| 52 | +<!-- tabs:start --> |
| 53 | + |
| 54 | +### **Python3** |
| 55 | + |
| 56 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 57 | + |
| 58 | +```python |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +### **Java** |
| 63 | + |
| 64 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 65 | + |
| 66 | +```java |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +### **C++** |
| 71 | + |
| 72 | +```cpp |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +### **Go** |
| 77 | + |
| 78 | +```go |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +### **...** |
| 83 | + |
| 84 | +``` |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +<!-- tabs:end --> |
0 commit comments