From 4e2ac3ac0718a415a518c0040db413d9d6e875f7 Mon Sep 17 00:00:00 2001 From: Ali Nawaz <110383490+AliPythonDev@users.noreply.github.com> Date: Sun, 19 May 2024 13:57:50 +0500 Subject: [PATCH] Create Solution.cpp --- .../Solution.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp new file mode 100644 index 0000000000000..3b048350a7666 --- /dev/null +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + long long maximumValueSum(vector& nums, int k, + vector>& edges) { + long long totalSum = 0; + int count = 0; + int positiveMin = INT_MAX; + int negativeMax = INT_MIN; + + for (int nodeValue : nums) { + int nodeValAfterOperation = nodeValue ^ k; + totalSum += nodeValue; + int netChange = nodeValAfterOperation - nodeValue; + + if (netChange > 0) { + positiveMin = min(positiveMin, netChange); + totalSum += netChange; + count += 1; + } else { + negativeMax = max(negativeMax, netChange); + } + } + + if (count % 2 == 0) { + return totalSum; + } + return max(totalSum - positiveMin, totalSum + negativeMax); + } +};