From b10dda2ae8d67900894c321596d6a66c341d2f94 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Fri, 17 Jul 2026 01:28:28 +0530 Subject: [PATCH] feat: POTD 2026-07-16 solved --- src/3800-3899/3867.cpp | 116 +++++++++++++++++++++++++++++++++++++++ tests/3800-3899/3867.txt | 13 +++++ 2 files changed, 129 insertions(+) create mode 100644 src/3800-3899/3867.cpp create mode 100644 tests/3800-3899/3867.txt diff --git a/src/3800-3899/3867.cpp b/src/3800-3899/3867.cpp new file mode 100644 index 0000000..6494c54 --- /dev/null +++ b/src/3800-3899/3867.cpp @@ -0,0 +1,116 @@ +/* +Tags +level-medium +array, math, two-pointers, sorting, simulation, number-theory + +Problem Description +3867. Sum of GCD of Formed Pairs + +You are given an integer array nums of length n. +Construct an array prefixGcd where for each index i: +Let mxi = max(nums[0], nums[1], ..., nums[i]). +prefixGcd[i] = gcd(nums[i], mxi). +After constructing prefixGcd: +Sort prefixGcd in 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 gcd of the two elements. +If n is odd, the middle element in the prefixGcd array remains unpaired and should be ignored. +Return an integer denoting the sum of the GCD values of all formed pairs. + +The term gcd(a, b) denotes the greatest common divisor of a and b. + +Example 1: +Input: nums = [2,6,4] +Output: 2 +Explanation: +Construct prefixGcd: +i nums[i] mxi prefixGcd[i] +0 2 2 2 +1 6 6 6 +2 4 6 2 +prefixGcd = [2, 6, 2]. After sorting, it forms [2, 2, 6]. +Pair the smallest and largest elements: gcd(2, 6) = 2. The remaining middle element 2 is ignored. Thus, the sum is 2. + +Example 2: +Input: nums = [3,6,2,8] +Output: 5 +Explanation: +Construct prefixGcd: +i nums[i] mxi prefixGcd[i] +0 3 3 3 +1 6 6 6 +2 2 6 2 +3 8 8 8 +prefixGcd = [3, 6, 2, 8]. After sorting, it forms [2, 3, 6, 8]. +Form pairs: gcd(2, 8) = 2 and gcd(3, 6) = 3. Thus, the sum is 2 + 3 = 5. + +Constraints: +1 <= n == nums.length <= 10^5 +1 <= nums[i] <= 10​​​​​​^​9 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../core/core.h" +using namespace std; + +class Solution +{ +public: + long long gcdSum(vector& nums) + { + int n = nums.size(); + vector prefixGcd(n, 0); + int maxVal = nums[0]; + for (int i = 0; i < n; i++) + { + maxVal = max(maxVal, nums[i]); + prefixGcd[i] = gcd(nums[i], maxVal); + } + + sort(prefixGcd.begin(), prefixGcd.end()); + int left = 0; + int right = n - 1; + long long result = 0; + while (left < right) + { + result += gcd(prefixGcd[left], prefixGcd[right]); + left++; + right--; + } + + return result; + } +}; + +int main() +{ + int n; + cin >> n; + vector nums(n, 0); + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + Solution sol; + long long result = sol.gcdSum(nums); + cout << result << "\n"; + return 0; +} \ No newline at end of file diff --git a/tests/3800-3899/3867.txt b/tests/3800-3899/3867.txt new file mode 100644 index 0000000..7425f10 --- /dev/null +++ b/tests/3800-3899/3867.txt @@ -0,0 +1,13 @@ +Test Case 1: +Input: +3 +2 6 4 +Output: +2 + +Test Case 2: +Input: +4 +3 6 2 8 +Output: +5 \ No newline at end of file