Generic K Sum | Easy to understand
\n\ninterface IKSum {\n\n    List<List<Integer>> kSum(int nums[], int k, int target);\n}\n\n/**\n * We\'ll use the problem of k sum and reduce it to two sum and
```\n\ninterface IKSum {\n\n    List<List<Integer>> kSum(int nums[], int k, int target);\n}\n\n/**\n * We\'ll use the problem of k sum and reduce it to two sum and attache the result back\n */\nclass KSumSorting implements IKSum {\n\n    @Override\n    public List<List<Integer>> kSum(int[] nums, int k, int target) {\n        if (nums == null || nums.length == 0 || nums.length < k)\n            return Collections.EMPTY_LIST;\n\n        Arrays.sort(nums);\n\n        return kSum(nums, k, 0, target);\n\n    }\n\n    private List<List<Integer>> kSum(int[] nums, int k, int start, int target) {\n        int len = nums.length;\n        List<List<Integer>> result = new ArrayList<>();\n        if (len < k)\n            return Collections.EMPTY_LIST;\n\n        if (k == 2) {\n            return twoSum(nums, start, len - 1, target);\n        } else {\n\n            /**\n             * Take each element, and exclude it from target to reduce the problem to smaller k-1 sum problem\n             */\n            for (int i = start; i < len - (k - 1); i++) {\n                /**\n                 * Avoid duplicates\n                 */\n                if (i > start && nums[i] == nums[i - 1])\n                    continue;\n\n                int ele = nums[i];\n\n                int rem = target - nums[i];\n                List<List<Integer>> smallerSumResult = kSum(nums, k - 1, i + 1, rem);\n\n                /**\n                 * Append the current element to make it k sum from k-1 sum\n                 */\n                for (List<Integer> list : smallerSumResult)\n                    list.add(0, ele);\n\n                /**\n                 * Append to our result\n                 */\n                result.addAll(smallerSumResult);\n\n\n            }\n        }\n\n        return result;\n    }\n\n    private List<List<Integer>> twoSum(int nums[], int low, int high, int target) {\n\n        List<List<Integer>> result = new ArrayList<>();\n\n        while (low < high) {\n\n            int sum = nums[low] + nums[high];\n\n            if (sum == target) {\n                List<Integer> twoSum = new ArrayList<>();\n                twoSum.add(nums[low]);\n                twoSum.add(nums[high]);\n\n                result.add(twoSum);\n\n                /**\n                 * Avoid duplicates\n                 */\n\n                while (low < high && nums[low] == nums[++low]) ;\n                while (high > low && nums[high] == nums[--high]) ;\n\n            } else if (sum < target)\n                low++;\n            else\n                high--;\n\n        }\n\n        return result;\n    }\n}\n\n```\n\nDriver code\n\n```\n\npublic class KSum {\n    public static void main(String[] args) {\n        test(new int[]{1, 0, -1, 0, -2},  4, 0);\n\n        test(new int[]{1, 0, -1, 0, -2, 2}, 4, 0);\n\n        test(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 4, 0);\n\n        test(new int[]{-4, -3, -2, -1, 0, 0, 1, 2, 3, 4}, 4, 0);\n\n\n        test(new int[]{1, 0, -1, 0, -2},  5, 0);\n\n        test(new int[]{1, 0, -1, 0, -2, 2}, 5, 0);\n\n        test(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 5, 0);\n\n        test(new int[]{-4, -3, -2, -1, 0, 0, 1, 2, 3, 4}, 5, 0);\n\n\n        test(new int[]{1, 0, -1, 0, -2},  3, 0);\n\n        test(new int[]{1, 0, -1, 0, -2, 2}, 3, 0);\n\n        test(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 3, 0);\n\n        test(new int[]{-4, -3, -2, -1, 0, 0, 1, 2, 3, 4}, 3, 0);\n\n\n\n        test(new int[]{1, 0, -1, 0, -2},  2, 0);\n\n        test(new int[]{1, 0, -1, 0, -2, 2}, 2, 0);\n\n        test(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 2, 0);\n\n        test(new int[]{-4, -3, -2, -1, 0, 0, 1, 2, 3, 4}, 2, 0);\n\n    }\n\n    private static void test(int[] nums, int k, int target) {\n        IKSum kSum = new KSumSorting();\n\n        System.out.println("\\nInput : " + Printer.toString(nums) + " K :" + k + " Target: " + target);\n        System.out.println("Sorting: " + kSum.kSum(nums, k, target));\n    }\n\n\n}\n```
[]