-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2941-maximum-gcd-sum-of-a-subarray.js
More file actions
57 lines (50 loc) · 1.5 KB
/
2941-maximum-gcd-sum-of-a-subarray.js
File metadata and controls
57 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 2941. Maximum GCD-Sum of a Subarray
* https://leetcode.com/problems/maximum-gcd-sum-of-a-subarray/
* Difficulty: Hard
*
* You are given an array of integers nums and an integer k.
*
* The gcd-sum of an array a is calculated as follows:
* - Let s be the sum of all the elements of a.
* - Let g be the greatest common divisor of all the elements of a.
* - The gcd-sum of a is equal to s * g.
*
* Return the maximum gcd-sum of a subarray of nums with at least k elements.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxGcdSum = function(nums, k) {
const prefixSum = [0];
let gcdPairs = [];
let result = 0;
for (let i = 0; i < nums.length; i++) {
prefixSum.push(prefixSum[prefixSum.length - 1] + nums[i]);
const newGcdPairs = [];
const candidates = [...gcdPairs, [i, nums[i]]];
for (const [startIndex, currentGcd] of candidates) {
const newGcd = gcd(currentGcd, nums[i]);
if (newGcdPairs.length === 0 || newGcdPairs[newGcdPairs.length - 1][1] !== newGcd) {
newGcdPairs.push([startIndex, newGcd]);
}
}
gcdPairs = newGcdPairs;
for (const [startIndex, gcdValue] of gcdPairs) {
if (i - startIndex + 1 < k) break;
const subarraySum = prefixSum[prefixSum.length - 1] - prefixSum[startIndex];
result = Math.max(result, subarraySum * gcdValue);
}
}
return result;
function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
};