649. Maximum GCD-Sum of a Subarray

You are given an array of integers nums and an integer k.

Define the gcd-sum of an array a as follows:

  • Let s be the total sum of all elements in a.
  • Let g be the greatest common divisor (GCD) of all elements in a.
  • The gcd-sum is calculated as s * g.

Your task is to find and return the maximum gcd-sum among all subarrays of nums that contain at least k elements.

Example 1:

Input : nums = [2, 1, 4, 4, 4, 2], k = 2

Output : 48

Explanation : The subarray [4, 4, 4] has a sum of 12 and a GCD of 4, resulting in a gcd-sum of 12 * 4 = 48.

Example 2:

Input : nums = [7, 3, 9, 4], k = 1

Output : 81

Explanation : The subarray [9] produces a sum of 9 and a GCD of 9, yielding a gcd-sum of 9 * 9 = 81.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [12, 15, 18, 6], k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= 106
  • 1 <= k <= n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long maxGcdSum(vector<int> &nums, int k) {
// Your code goes here
}
};
Test Case

Input:

K
Nums