👏💯PYTHON 🎉|| JS 🎉|| JAVA 🎉|| 🔥🔥 Best Visualization  ||✅ New Question || 🫰Only Sorting
Screenshot \uD83C\uDF89\n\n\n\n\n---\n\n\n# Intuition \uD83E\uDD14\n Describe your first thoughts on how to solve this problem. \n    maximum possible sum of th
# Screenshot \uD83C\uDF89\n\n![3068.png](https://assets.leetcode.com/users/images/a2eea421-3d0c-4414-bea2-564ce65e7047_1716103426.4556427.png)\n\n\n---\n\n\n# Intuition \uD83E\uDD14\n<!-- Describe your first thoughts on how to solve this problem. -->\n    maximum possible sum of the values \n\n    Let\'s understand the problem first and make it easy\n\n1. `Given` \n    `A.` An `undirected tree` with `n Node` whose value are `0 to n - 1`\n\n    `B.` Value of tree can be our `index value`\n\n    `C.` `Array (nums)` which actually tells us the `exact value of node`\n\n    `D.` A number `k` by which `we have to take XOR` with `tree value`\n\n2. `Work to do`\n\n    `A.` we\'ve to `select any edge` from tree\n\n    `B.` Then `take XOR` with `both the connected node` with that edge\n\n    `C.` and then `sum all actual values` of node from `given nums array`\n\n3. `Return the sum` of all value `after taking after xor`\n\n    ### We have to maximize this sum ---------\n---\n\n\n# Approach \uD83E\uDD73\n<!-- Describe your approach to solving the problem. -->\n    Break this in a very simple problem ...\n\n1. Let\'s `understand the XOR` first\n \n   `A.` `1 time XOR` --> It give s`one value either greater or lower or 0`\n        10 ^ 2 = 8  \n\n    `B.` `2 time XOR` --> It give the `same value`\n            10 ^ 2 ^ 2 = 10\n\n2. Solve `by understanding problem` -->  `K = 3`\n    \n  \n    ![que 3068.png](https://assets.leetcode.com/users/images/b2b79abd-f1d7-42cc-aba5-43bd4ecb1f31_1716114204.9783974.png)\n\n    `A.` First we\'ll select `( A C D E edge)` \n\n    `B.`  Take the `xor of value` with **`k = 3`**\n\n    `C.` only `Take XOR or that value which give a large value`\n\n    `D.`We did not take XOR of 3 because `it\'ll give 0` (Lower value)\n    \n    \n    ![que 1 3068.png](https://assets.leetcode.com/users/images/040db16f-e3e4-454e-bd41-50f30a040179_1716114233.129734.png)\n\n\n    `E.` `sum will be 39`\n\n---\n\n3. `As we see node 0 does not mean anything to us`\n\n    ```\n            // Best appoach\n\n            1. Either take one edge or just take 2 node\n            2. Take one node and do not touch it\n            3. Or take any 2 node and take \n\n            --> We can\'t just take every node and take xor of that\n    ```\n    ![que 2 3068.png](https://assets.leetcode.com/users/images/7e45327f-52ea-444f-bfdd-b88ad9cdc1e6_1716114755.572279.png)\n\n        Take 1st and 3th at a time\n            and\n        Take 4th and 5th at a time\n            and\n        Do not touch any node\n\n4. Now `problem is that how to choose our node`\n\n        Simple -> take XOR if value inc take it otherwise leave it \n\n---\n\n# Understand with code \uD83E\uDD73\n\n    We will use best Approach method\n\n    - In which we have 5 works to do\n    \n    1. Make one diff array give us values if inc or dec after XOR.\n    2. Sort this array because we need only increased values\n    3. take sum of previous values\n    4. Add the positive that comes after the xor from our XOR array\n    5. add 2 values at a time as we guessed that and return the some\n---\n\n`Step 1` **Make a xorArray** \n\n    Which will store if value inc or dec either +ve or -ve\n\n    Take XOR of value and given K and subtract with itself \n\n    So It will give the diffrence only either +Ve or -ve or 0\n\n```python []\n     xor_arr = [(n ^ k) - n for n in nums]\n```\n```JAVASCRIPT []\n    let xorArr = nums.map(n => (n ^ k) - n)\n\n```     \n```JAVA []\n    public Integer[] xorArrCreator(int arr[], int k) {\n        Integer xorArr[] = new Integer[arr.length];\n\n        for (int i = 0; i < arr.length; i++) {\n            xorArr[i] = (arr[i] ^ k) - arr[i];\n        }\n\n        return xorArr;\n    }\n\n    // function will return our require array\n    Integer xorArr[] = xorArrCreator(nums, k);\n```\n\n---\n\n`Step 2 - 3` **Sort in reverse order and take the sum of nums array** \n\n    because we just need only +ve values\n\n```python []\n    xor_arr.sort(reverse=True)\n    res = sum(nums)\n\n```\n```JAVASCRIPT []\n    xorArr.sort((a, b) => b - a);\n    let sum = nums.reduce((a, b) => a + b);\n```     \n```JAVA []\n    // Give our sum of array\n    public long numArrSum(int arr[]) {\n        long sum = 0;\n\n        for (int i = 0; i < arr.length; i++) {\n            sum += arr[i];\n        }\n\n        return sum;\n    }\n\n    // Sort in reverse order\n    Arrays.sort(xorArr, Collections.reverseOrder());\n\n    // taking sum of all values in nums array\n    long sum = numArrSum(nums);\n```\n---\n\n`Step 4` **Apply loop and add values 2 at a time** \n\n    allthough we are adding 2 values at a time \n\n    In case of odd it will leave one value \n\n    But It will not gonna add because if we add that node \n    \n    it will dec other node connected with same edge\n\n```python []\n    for i in range(0, len(nums), 2):\n        if i == len(nums) - 1:\n            break\n\n        path_sum = xor_arr[i] + xor_arr[i + 1]\n\n        if path_sum <= 0:\n            break\n\n        res += path_sum\n```\n```JAVASCRIPT []\n    for (let i = 0; i < nums.length; i += 2) {\n        if (i == nums.length - 1) {\n                break;\n        }\n\n        let pathSum = xorArr[i] + xorArr[i + 1]\n\n        if (pathSum <= 0) {\n                break;\n        }\n\n        sum += pathSum;\n    }\n```     \n```JAVA []\n    for (int i = 0; i < xorArr.length; i += 2) {\n        if (i == xorArr.length - 1) {\n            break;\n        }\n        long pathSum = xorArr[i] + xorArr[i + 1];\n        if (pathSum <= 0) {\n            break;\n        }\n        sum += pathSum;\n    }\n```\n---\n\n`Step 5` **Make a xorArray** \n\n    return sum\n\n```python []\n    return res\n\n```\n```JAVASCRIPT []\n    return sum\n\n```     \n```JAVA []\n    return sum;\n```\n\n---\n\n# Complexity \uD83D\uDD25\n- Time complexity: $$O(nlogn)$$ `For sorting`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ `For Array `\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n# Code\n\n```PYTHON []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        xor_arr = [(n ^ k) - n for n in nums]\n        xor_arr.sort(reverse=True)\n        res = sum(nums)\n\n        for i in range(0, len(nums), 2):\n            if i == len(nums) - 1:\n                break\n\n            path_sum = xor_arr[i] + xor_arr[i + 1]\n\n            if path_sum <= 0:\n                break\n\n            res += path_sum\n\n        return res\n\n```\n```JAVASCRIPT []\nvar maximumValueSum = function (nums, k, edges) {\n      let xorArr = nums.map(n => (n ^ k) - n)\n      xorArr.sort((a, b) => b - a);\n      let sum = nums.reduce((a, b) => a + b);\n\n      for (let i = 0; i < nums.length; i += 2) {\n            if (i == nums.length - 1) {\n                  break;\n            }\n\n            let pathSum = xorArr[i] + xorArr[i + 1]\n\n            if (pathSum <= 0) {\n                  break;\n            }\n\n            sum += pathSum;\n      }\n\n      return sum\n};\n```\n```JAVA []\nclass Solution {\n\n    public Integer[] xorArrCreator(int arr[], int k) {\n        Integer xorArr[] = new Integer[arr.length];\n\n        for (int i = 0; i < arr.length; i++) {\n            xorArr[i] = (arr[i] ^ k) - arr[i];\n        }\n\n        return xorArr;\n    }\n\n    public long numArrSum(int arr[]) {\n        long sum = 0;\n\n        for (int i = 0; i < arr.length; i++) {\n            sum += arr[i];\n        }\n\n        return sum;\n    }\n\n    // Main problem solver fun\n    public long maximumValueSum(int[] nums, int k, int[][] edges) {\n        // Making xorArr\n        Integer xorArr[] = xorArrCreator(nums, k);\n\n        // Sort in reverse order\n        Arrays.sort(xorArr, Collections.reverseOrder());\n\n        // taking sum of all values in nums array\n        long sum = numArrSum(nums);\n\n        for (int i = 0; i < xorArr.length; i += 2) {\n            if (i == xorArr.length - 1) {\n                break;\n            }\n            long pathSum = xorArr[i] + xorArr[i + 1];\n            if (pathSum <= 0) {\n                break;\n            }\n            sum += pathSum;\n        }\n\n        return sum;\n    }\n}\n```\n\n
['Array', 'Dynamic Programming', 'Greedy', 'Bit Manipulation', 'Tree', 'Sorting', 'Python', 'Java', 'JavaScript']