My solution generalized for kSums in JAVA
General Idea\nIf you have already read and implement the 3sum and 4sum by using the sorting approach: reduce them into 2sum at the end, you might already got th
#### General Idea\nIf you have already read and implement the 3sum and 4sum by using the sorting approach: reduce them into 2sum at the end, you might already got the feeling that, all ksum problem can be divided into two problems: \n1. 2sum Problem\n2. Reduce K sum problem to K \u2013 1 sum Problem\n\nTherefore, the ideas is simple and straightforward. We could use recursive to solve this problem. Time complexity is O(N^(K-1)).\n\n```JAVA\n    public class Solution {\n        int len = 0;\n        public List<List<Integer>> fourSum(int[] nums, int target) {\n            len = nums.length;\n            Arrays.sort(nums);\n            return kSum(nums, target, 4, 0);\n        }\n       private ArrayList<List<Integer>> kSum(int[] nums, int target, int k, int index) {\n            ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();\n            if(index >= len) {\n                return res;\n            }\n            if(k == 2) {\n            \tint i = index, j = len - 1;\n            \twhile(i < j) {\n                    //find a pair\n            \t    if(target - nums[i] == nums[j]) {\n            \t    \tList<Integer> temp = new ArrayList<>();\n                    \ttemp.add(nums[i]);\n                    \ttemp.add(target-nums[i]);\n                        res.add(temp);\n                        //skip duplication\n                        while(i<j && nums[i]==nums[i+1]) i++;\n                        while(i<j && nums[j-1]==nums[j]) j--;\n                        i++;\n                        j--;\n                    //move left bound\n            \t    } else if (target - nums[i] > nums[j]) {\n            \t        i++;\n                    //move right bound\n            \t    } else {\n            \t        j--;\n            \t    }\n            \t}\n            } else{\n                for (int i = index; i < len - k + 1; i++) {\n                    //use current number to reduce ksum into k-1sum\n                    ArrayList<List<Integer>> temp = kSum(nums, target - nums[i], k-1, i+1);\n                    if(temp != null){\n                        //add previous results\n                        for (List<Integer> t : temp) {\n                            t.add(0, nums[i]);\n                        }\n                        res.addAll(temp);\n                    }\n                    while (i < len-1 && nums[i] == nums[i+1]) {\n                        //skip duplicated numbers\n                        i++;\n                    }\n                }\n            }\n            return res;\n        }\n    }\n```
['Java']