Rudimentary explanation for any language / code in go
Code\n\ntype Item struct  {\n    Sum int64\n    Index int\n}\n\nfunc kSum(nums []int, k int) int64 {\n\n    // =================================================
# Code\n```\ntype Item struct  {\n    Sum int64\n    Index int\n}\n\nfunc kSum(nums []int, k int) int64 {\n\n    // ===========================================================================================\n\n    // I have never really worked on LeetCode before, yet somehow stumbled on this problem and I decided to figure this out\n    // I got pretty close, but failed to find a algo that works for all testcase (my primitive solution got up to 12)\n    // After severals days I decided to look at people\'s solutions and they are mostly consonant with my findings\n    // ...Except they are extremyly clean! There is clearly some knowledge gap here\n    // I\'m the type of people that when learning stuff I can not move on if I don\'t understand how things works\n    // So I digged in more and eventually sorted it out\n    // And I decided to write a explanation that can be understand by anyone with rudimentary knowledege\n    \n    // Given [1,-2,3,4,-10,12]\n    // The sum of this consequense is 8, but if we ignore negative numbers, it would be 20, which is 8 -(-2) -(-10), which means we are excluding -2 and -10\n    // So we know that the max possible sum is always the sum of all positive numbers\n    // What could be the next largest sum, apparently, if we exclude the smallest positive number (1), we get 19\n    // Then... what could be the next?\n    // A) Excluding 3 we get 17\n    // B) Re-including the excluded -2, we get 18\n    // C) Excluding 1 and Re-including the excluded -2, we get 17\n    // It turns out, because our max sum comes from excluding negative numbers and including positive ones\n    // Picking a positive number means removing it from our max sum, and pick a negative number means adding it to our max sum\n    // In other words, picking a number means subtracting its absolute value from our max sum\n    // [1,-2,3,4,-10,12] is not much differnt from [1,2,3,4,10,12] in this case\n\n    // We are looking for Kth largest sum\n    // This means we are repeating K times the process of finding the next number combination that gives the smallest sum to exclude\n    \n    // How do we find the next combination which gives the smallest sum?\n    // If we sort the absolute numbers consequence from smallest to largest\n    // Say [1, 2, 3, 4, 10, 12]\n    // It\'s easy to notice that, the fewer numbers to the right included, the sum will be smaller\n    // For example: [1, 2] is smaller than [1, 2, 3]\n    // For example: [2, 3] is smaller than [3, 4]\n    \n    // But because the numbers can be anything as long as they are bigger than their left neighbours\n    // Combinations of more amount of numbers are not guaranteed to sum bigger than combinations of fewer amount of numbers\n    // Say [1, 2, 3, 4], [1, 2] >= [3] but not [4], [1, 2, 3] >= [2,4] but < [3, 4]\n\n    // Before we continue, we need to know how do we iterate through all possible combinations\n    // To iterate all subconsequences of a given consequence of N numbers, Say [1, 2, 3] (N=3)\n    // We can break it down to iterations of all N=1, N=2 and N=3 subconsequences\n    // How do we iterate through all subconsequences of a given N?\n    // We swap the tail!\n    // In the subconsequences of single number: [1], [2], [3], the numbers are tails, and the head is []-- yes, an empty head.\n    // In the subconsequences of 2 number: [1, 2] [1, 3], Here 2 and 3 are the tails, [1,] is the head.\n\n    // So we iterate through all subconsequences of 1 numbers\n    // then 2 numbers, then 3... all the way to the final combination of absolute of all given numbers (ex: [1,2,3] of [1,2,3])\n    // This way we can iterate through all possible combinations\n\n    // However, it\'s not practical to simply iterate through all possible combinations and sort the sums\n    // For any given consequence, every number is either included or not included\n    // This means there would be 2^N possible consequences, it\'s unacceptable both cpu and memory wise\n\n    // Therefore, we\'ll need a way to step through combinations while always picking the next one that gives the smallest sum\n    // This is NOT possible without keeping track of all combinations we\'ve iterated through\n\n    // Luckily, we\'ve got everything we need to get this solved.\n\n    // Because the absolute numbers consequence from smallest to largest\n    // The next combination of smallest sum TO THE CURRENT COMBINATION ALWAYs comes from either:\n    // A) Swapping the tail of current combination to the next larger number\n    // B) Increase N (the numbers of combinations) and include the next larger number to current combination \n\n    // Any other combination would be definitely bigger than these 2 possibilities\n\n    // Say we\'re given a 6-nums consequence\n    // Say [1, 2, 3, 4, 10, 12] (Which is sorted absolute of [1,-2,3,4,-10,12])\n    // We already know that the max sum if [1,-2,3,4,-10,12] is 20\n    // Which is the sum of only positive numbers\n    // Which actually means we are excluding an empty combination []\n\n    // Now, we are looking for the next combination of smallest sum\n    // Because we got an empty combination [] and got not tail to swap\n    // We\'ll try B: Increase N (the numbers of combinations) and include the next larger number\n    // This will give us [1]\n    // We can now go A or B for the next possibility\n    // Route A gives us [2] (which is the result of 1 getting swapped out)\n    // Route B gives us [1,2]\n    \n    // Of course we pick [2] because it sums smaller than [1,2] (1+2=3)\n\n    // What now? We got [2] as our current combination\n    // Route A gives us [3] (which is the result of 2 getting swapped out)\n    // Route B gives us [2,3]\n\n    // We should just pick [3] because it sums smaller than [2,3] (2+3=5), right?\n    // NO, because while [3] sums smaller than [2,3]\n    // It sums equal to [1,2], which is left in previous comparison\n\n    // That\'s where priority queue or min/max heap comes in\n    // They are just a storage of all combinations we\'ve iterated through and it\'s eaasy to access the max or min\n\n    // In this case it\'s a equal, so we can pick either one, but this would not always be true\n\n    // Let\'s pick [1,2] this time\n    // Route A gives us [1,3] (which is the result of 2 getting swapped out)\n    // Route B gives us [1,2,3]\n\n    // We\'ll going to pick [1,3] because it sums smaller than [1,2,3] and [2,3]\n    // And then we continue...\n\n    // But we\'ll not continue the simulation here\n    // The above is enough to demonstrate the idea and explain people\'s solutions\n\n    // ===========================================================================================\n\n    maxSum := int64(0) // The biggest sum of subsequences (all positive)\n    for i := 0; i < len(nums); i++ { // sort by absolute values\n        if (nums[i] > 0) {\n            maxSum += int64(nums[i])\n        }\n        if (nums[i] < 0) {\n            nums[i] *= -1\n        }\n    }\n\n    // Inefficient sorting will results in exceeding time limit\n    mergeSort(nums, 0, len(nums))\n\n    // Dumb array storage and operation also causes timeout\n    dumbHeap := make([]Item, 0, k)\n    dumbHeap = minHeapInsert(dumbHeap, Item{Sum: int64(nums[0]), Index: 0})\n    k -= 1 // We\'ve got 0th sum (max sum)\n\n    var taking Item\n\n    for x := 0; x < k; x++ {\n        dumbHeap, taking = minHeapTake(dumbHeap)\n\n        if taking.Index < len(nums) - 1 {\n            a := Item { Sum: taking.Sum + int64(-nums[taking.Index] + nums[taking.Index + 1]), Index: taking.Index + 1 } // Route A (Swapping the tail)\n            b := Item { Sum: taking.Sum + int64(nums[taking.Index + 1]), Index: taking.Index + 1 } // Route B (Include more number)\n\n            dumbHeap = minHeapInsert(dumbHeap, a)\n            dumbHeap = minHeapInsert(dumbHeap, b)\n\n            if len(dumbHeap) > k*2+1 {\n                dumbHeap = dumbHeap[:k*2+1]\n            }\n        }\n    }\n\n    return maxSum - taking.Sum\n}\n\nfunc mergeSort(array []int, left, right int) {\n\tif left >= right-1 {\n\t\treturn\n\t}\n\n\tif right-left == 2 {\n\t\tif array[left] > array[right-1] {\n\t\t\tarray[left], array[right-1] = array[right-1], array[left]\n\t\t\treturn\n\t\t}\n\t}\n\n\tmid := (right + left) / 2\n\tmergeSort(array, left, mid)\n\tmergeSort(array, mid, right)\n\n\tcache := make([]int, right-left)\n\tcopy(cache, array[left:])\n\tleftSub := cache[:mid-left]\n\trightSub := cache[mid-left : right-left]\n\n\tmerged := left\n\tidLeft, idRight := 0, 0\n\tfor idLeft < len(leftSub) && idRight < len(rightSub) {\n\t\tif leftSub[idLeft] <= rightSub[idRight] {\n\t\t\tarray[merged] = leftSub[idLeft]\n\t\t\tidLeft++\n\t\t} else {\n\t\t\tarray[merged] = rightSub[idRight]\n\t\t\tidRight++\n\t\t}\n\t\tmerged++\n\t}\n\n\tfor idLeft < len(leftSub) {\n\t\tarray[merged] = leftSub[idLeft]\n\t\tidLeft++\n\t\tmerged++\n\t}\n\n\tfor idLeft < len(leftSub) {\n\t\tarray[merged] = leftSub[idLeft]\n\t\tidLeft++\n\t\tmerged++\n\t}\n}\n\n\nfunc minHeapInsert (heap []Item, item Item) []Item {\n    // In complete binary trees stored as an array\n    // For any element at index N, its parent is at index (N-1)/2 and its children are at index 2N+1 and 2N+2\n\n    heap = append(heap, item)\n\n    for cursor := len(heap) -1;cursor > 0;{\n        parentId := (cursor-1)/2\n        if heap[parentId].Sum > heap[cursor].Sum {\n            heap[parentId], heap[cursor] = heap[cursor], heap[parentId]\n        } else {\n            break\n        }\n        cursor = parentId\n    }\n\n    return heap\n}\n\nfunc minHeapTake (heap []Item) ([]Item, Item) {\n    taking := heap[0]\n    heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0]\n    heap = heap[:len(heap)-1]\n    for cursor := 0;; {\n        leftChildId := cursor*2+1\n        rightChildId := cursor*2+2\n        swapping := leftChildId\n        if rightChildId < len(heap) && heap[rightChildId].Sum < heap[leftChildId].Sum {\n            swapping = rightChildId\n        }\n        if swapping >= len(heap) {\n            break\n        }\n        if heap[swapping].Sum < heap[cursor].Sum {\n            heap[swapping], heap[cursor] = heap[cursor], heap[swapping]\n            cursor = swapping\n        } else {\n            break\n        }\n    }\n    return heap, taking\n}\n```
['Go']