3867. Sum of GCD of Formed Pairs
You are given an integer array
numsof lengthn.Construct an array
prefixGcdwhere for each indexi:
- Let
mx_i = max(nums[0], nums[1], ..., nums[i]).prefixGcd[i] = gcd(nums[i], mx_i).After constructing
prefixGcd:
- Sort
prefixGcdin non-decreasing order.- Form pairs by taking the smallest unpaired element and the largest unpaired element.
- Repeat this process until no more pairs can be formed.
- For each formed pair, compute the
gcdof the two elements.- If
nis odd, the middle element in theprefixGcdarray remains unpaired and should be ignored.Return an integer denoting the sum of the GCD values of all formed pairs.
gcd(a, b)
greatest common divisor
a
b
1 | class Solution { |