🔥 🔥 🔥 Fastest (100%) || Video Explanation || Easy to understand 🔥 🔥 🔥
Detailed Approach Explained in Video Here\n\n\n\n\n# Intuition\n- The intuition behind this approach is that XOR operations on elements in the list can either i
# [Detailed Approach Explained in Video Here](https://www.youtube.com/watch?v=bmwQtId0Z2Q)\n\n![Screenshot 2024-05-18 at 6.24.53\u202FPM.png](https://assets.leetcode.com/users/images/edde47ab-144e-4f6c-bd69-c8b9d3eda0bb_1716078327.8983808.png)\n\n\n# Intuition\n- The intuition behind this approach is that XOR operations on elements in the list can either increase or decrease their values. The goal is to maximize the overall sum after these operations. By tracking the minimum positive change and the maximum negative change, the solution ensures that if the count of beneficial operations (positive changes) is odd, we can adjust the sum to ensure it remains maximized.\n\n# Approach\n# [Detailed Approach Explained in Video Here](https://www.youtube.com/watch?v=bmwQtId0Z2Q)\n\n**Initialization:**\n\n- totalSum keeps track of the total sum of elements after potential XOR operations.\n- count tracks the number of elements that were increased by the XOR operation.\n- positiveMin is initialized to infinity and will store the minimum positive net change from the XOR operation.\n- negativeMax is initialized to negative infinity and will store the maximum negative net change from the XOR operation.\n\n**Iterating Through Elements:**\n\n- For each element in nums, compute the result of XOR-ing the element with k (i.e., nodeValAfterOperation = nodeValue ^ k).\n- Calculate the netChange which is the difference between the XOR-ed value and the original value (netChange = nodeValAfterOperation - nodeValue).\n\n**Updating totalSum:**\n\n- Add the original value of the element to totalSum.\n- If the netChange is positive (i.e., the XOR operation results in a higher value), update positiveMin if this change is the smallest positive change encountered and add this net change to totalSum.\n- If the netChange is negative, update negativeMax if this change is the largest negative change encountered.\n\n**Counting Operations:**\n\n- Increment the count for each positive net change since we are tracking how many elements were beneficially changed by the XOR operation.\nBalancing the Count of Changes:\n\n- If the count (number of beneficial changes) is even, return totalSum directly since an even count of positive changes ensures the sum is maximized.\n\n- If the count is odd, we face a dilemma since adding or subtracting an odd number of beneficial changes can be suboptimal. Hence, we need to decide whether to:\n    - Remove the smallest positive change to make the count even (maximizing totalSum - positiveMin).\n    - Add the largest negative change (potentially minimizing the loss) to balance out the sum (maximizing totalSum + negativeMax).\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```Python []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        totalSum = 0\n        count = 0\n        positiveMin = float("inf")\n        negativeMax = float("-inf")\n\n        for nodeValue in nums:\n            nodeValAfterOperation = nodeValue ^ k\n\n            totalSum += nodeValue\n            netChange = nodeValAfterOperation - nodeValue\n\n            if netChange > 0:\n                positiveMin = min(positiveMin, netChange)\n                totalSum += netChange\n                count += 1\n            else:\n                negativeMax = max(negativeMax, netChange)\n\n        if count % 2 == 0:\n            return totalSum\n        return max(totalSum - positiveMin, totalSum + negativeMax)\n\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k,\n                              vector<vector<int>>& edges) {\n        long long totalSum = 0;\n        int count = 0;\n        int positiveMin = INT_MAX;\n        int negativeMax = INT_MIN;\n\n        for (int nodeValue : nums) {\n            int nodeValAfterOperation = nodeValue ^ k;\n            totalSum += nodeValue;\n            int netChange = nodeValAfterOperation - nodeValue;\n\n            if (netChange > 0) {\n                positiveMin = min(positiveMin, netChange);\n                totalSum += netChange;\n                count += 1;\n            } else {\n                negativeMax = max(negativeMax, netChange);\n            }\n        }\n\n        if (count % 2 == 0) {\n            return totalSum;\n        }\n        return max(totalSum - positiveMin, totalSum + negativeMax);\n    }\n};\n```\n```Java []\nclass Solution {\n    public long maximumValueSum(int[] nums, int k, int[][] edges) {\n        long totalSum = 0;\n        int count = 0;\n        int positiveMin = Integer.MAX_VALUE;\n        int negativeMax = Integer.MIN_VALUE;\n\n        for (int nodeValue : nums) {\n            int nodeValAfterOperation = nodeValue ^ k;\n            totalSum += nodeValue;\n            int netChange = nodeValAfterOperation - nodeValue;\n\n            if (netChange > 0) {\n                positiveMin = Math.min(positiveMin, netChange);\n                totalSum += netChange;\n                count += 1;\n            } else {\n                negativeMax = Math.max(negativeMax, netChange);\n            }\n        }\n\n        if (count % 2 == 0) {\n            return totalSum;\n        }\n        return Math.max(totalSum - positiveMin, totalSum + negativeMax);\n    }\n}\n```\n```Javascript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number[][]} edges\n * @return {number}\n */\nvar maximumValueSum = function (nums, k, edges) {\n    let totalSum = 0;\n    let count = 0;\n    let positiveMin = Infinity;\n    let negativeMax = -Infinity;\n\n    for (let nodeValue of nums) {\n        let nodeValAfterOperation = nodeValue ^ k;\n        totalSum += nodeValue;\n        let netChange = nodeValAfterOperation - nodeValue;\n\n        if (netChange > 0) {\n            positiveMin = Math.min(positiveMin, netChange);\n            totalSum += netChange;\n            count += 1;\n        } else {\n            negativeMax = Math.max(negativeMax, netChange);\n        }\n    }\n\n    if (count % 2 === 0) {\n        return totalSum;\n    }\n    return Math.max(totalSum - positiveMin, totalSum + negativeMax);\n};\n```\n```Go []\nfunc maximumValueSum(nums []int, k int, edges [][]int) int64 {\n\tvar totalSum int64\n\tcount := 0\n\tpositiveMin := math.MaxInt64\n\tnegativeMax := math.MinInt64\n\n\tfor _, nodeValue := range nums {\n\t\tnodeValAfterOperation := nodeValue ^ k\n\t\ttotalSum += int64(nodeValue)\n\t\tnetChange := nodeValAfterOperation - nodeValue\n\n\t\tif netChange > 0 {\n\t\t\tif netChange < positiveMin {\n\t\t\t\tpositiveMin = netChange\n\t\t\t}\n\t\t\ttotalSum += int64(netChange)\n\t\t\tcount++\n\t\t} else {\n\t\t\tif netChange > negativeMax {\n\t\t\t\tnegativeMax = netChange\n\t\t\t}\n\t\t}\n\t}\n\n\tif count%2 == 0 {\n\t\treturn totalSum\n\t}\n\treturn int64(math.Max(float64(totalSum-int64(positiveMin)), float64(totalSum+int64(negativeMax))))\n}\n```\n```Kotlin []\nclass Solution {\n    fun maximumValueSum(nums: IntArray, k: Int, edges: Array<IntArray>): Long {\n        var totalSum: Long = 0\n        var count = 0\n        var positiveMin = Int.MAX_VALUE\n        var negativeMax = Int.MIN_VALUE\n\n        for (nodeValue in nums) {\n            val nodeValAfterOperation = nodeValue xor k\n            totalSum += nodeValue\n            val netChange = nodeValAfterOperation - nodeValue\n\n            if (netChange > 0) {\n                positiveMin = minOf(positiveMin, netChange)\n                totalSum += netChange\n                count += 1\n            } else {\n                negativeMax = maxOf(negativeMax, netChange)\n            }\n        }\n\n        return if (count % 2 == 0) {\n            totalSum\n        } else {\n            maxOf(totalSum - positiveMin, totalSum + negativeMax)\n        }\n    }\n}\n```\n\n# **Please Upvote if you like the solution**
['C++', 'Java', 'Go', 'Python3', 'JavaScript']