Find K-Sum Subsets
Find K-Sum Subsets asks you to return every subset of distinct positive integers whose elements add up exactly to a given target value.
We'll cover the following...
Statement
Given an array of k.
Return a 2D array, where each inner array represents a subset whose sum equals k.
Constraints:
, where is any member of the input array. k
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Find K-Sum Subsets
From the given set, find all subsets with sum equals k:
set = {8, 13, 3, 22, 17, 39, 87, 45, 36}
k = 16
{8, 3}
{13, 3}
{13, 8}
{22, 17}
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.
Try it yourself
Implement your solution in the following coding playground.
We have left the solution to this challenge as an exercise for you. An optimal solution to this problem runs in O(n * 2n) time and takes O(n) space. You may try to translate the logic of the solved puzzle into a coded solution.
import java.util.*;public class Solution{public static List<List<Integer>> getKSumSubsets(int[] nums, int k) {// Replace this placeholder return statement with your codereturn new ArrayList<>();}}