Python Generalized Soln  for ksum with explanation
Lets generalize  1sum,2sum,3sum,4sum... problems into ksum\n1)As we know we that 1sum is simply as returning the elements in the lists which are equal to target
Lets generalize  1sum,2sum,3sum,4sum... problems into ksum\n1)As we know we that 1sum is simply as returning the elements in the lists which are equal to target\n2)First we sort the given list then\n3)We can solve ksums where k>2 by reducing them to 2sum recursively\n4)Where we solve 2sum problem with two pointer approach\n```\nres = []\ni = begin #left pointer\nj = len(nums) - 1 #right pointer\nwhile i < j: #where pointers dont cross each other\n\tif nums[i] + nums[j] == target: \n\t\tres.append([nums[i], nums[j]]) # if found then nums[i] and nums[j] pair are one of the results hence we store these as pair\n\t\twhile i < j and nums[i] == nums[i + 1]:    #to remove redundant pairs\n\t\t\ti += 1\n\t\twhile i < j and nums[j] == nums[j - 1]: #to remove redundant pairs\n\t\t\tj -= 1\n\t\ti += 1\n\t\tj -= 1\n\t\t#as we found the pair we move left pointer forward and right pointer backward in list\n\telif nums[i] + nums[j] > target:\n\t\tj -= 1\n\t\t#As the sum is > target we move right pointer holding larger value to 1 step left\n\telse:\n\t\ti += 1\n\t\t#As the sum is < target we move left pointer smaller value to 1 step right\n\n```\n\n5)For 3sum we iterate over each element and find 2sum solution .ie if current index is i and element is nums[i]\n\twe find 2sum solution for target = target- nums[i] in list from index i+1 to i = len(nums)-1 and then add nums[i] to the solution\n6)For ksum we iterate over each element and find (k-1)sum solution recursively.\n\n```\nclass Solution:\n    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:\n        def helper(nums,target,ksum,begin):\n            if begin >= len(nums): #base condition\n                return []\n            if ksum == 2:  \n\t\t\t #two pointer approach as we do in 2sum problem\n                res = []\n                i = begin\n                j = len(nums) - 1\n                while i < j:\n                    if nums[i] + nums[j] == target:\n                        res.append([nums[i], nums[j]])\n                        while i < j and nums[i] == nums[i + 1]:\n                            i += 1\n                        while i < j and nums[j] == nums[j - 1]:\n                            j -= 1\n                        i += 1\n                        j -= 1\n                    elif nums[i] + nums[j] > target:\n                        j -= 1\n                    else:\n                        i += 1\n                return res\n            res = [] \n            for i in range(begin, len(nums) - ksum + 1):\n                if i > begin and nums[i] == nums[i - 1] or nums[i] + nums[-1] * (ksum - 1) < target:\n                    continue\n\t\t\t\t\t# the first condition is to remove Duplicates \n\t\t\t\t\t# the second Condition is that if we consider element nums[i] whether we get required target or not\n\t\t\t\t\t#ie if we consider nums[i] which is smallest in current ksum size windown and ksum-1 times of largest number in the nums and still we get sum < target the we do not need to consider nums[i]\n                if nums[i] + nums[i + 1] * (ksum - 1) > target:\n                    break\n\t\t\t\t\t#Same as above   if nums[i] + nums[i + 1] * (ksum - 1) > target it means that remaining elements after i algo give result > target hence we exit from the loop\n                r = helper(nums, target - nums[i], ksum - 1, i + 1)\n                for elm in r:\n                    elm.insert(0,nums[i])\n\t\t\t\t\t#We insert current nums[i] to the result of ksum-1 result\n                for elm in r:\n                    res.append(elm)\n\t\t\t\t\t#we store result of size ksum \n            return res\n\n        nums.sort()\n\t\tksum = 4\n\t\t#ksum = 4 As we are finding 4sum here\n\t\t# For generailized  case \n\t\t#if ksum <1: return []\n\t\t#if ksum ==1:\n\t\t\t# return  [target]*nums.count(target)\n        return helper(nums, target, ksum, 0)\n
[]