Subset Sum is NP Complete
Last Updated :
02 Nov, 2023
Prerequisite: NP-Completeness, Subset Sum Problem
Subset Sum Problem: Given N non-negative integers a1...aN and a target sum K, the task is to decide if there is a subset having a sum equal to K.
Explanation: An instance of the problem is an input specified to the problem. An instance of the subset sum problem is a set S = {a1, ..., aN} and an integer K. Since an NP-complete problem is a problem which is both in NP and NP-hard, the proof for the statement that a problem is NP-Complete consists of two parts:
- The problem itself is in NP class.
- All other problems in NP class can be polynomial-time reducible to that. (B is polynomial-time reducible to C is denoted as B ≤ PC)
If the 2nd condition is only satisfied then the problem is called NP-Hard.
But it is not possible to reduce every NP problem into another NP problem to show its NP-Completeness all the time. That is why if we want to show a problem is NP-Complete we just show that the problem is in NP and any NP-Complete problem is reducible to that then we are done i.e. if B is NP-Complete and B≤PC for C in NP, then C is NP-Complete. Thus, we can verify that the Subset Sum Problem is NP-Complete using the following two propositions:
Subset Sum is in NP:
If any problem is in NP, then given a certificate, which is a solution to the problem and an instance of the problem (a set S of integer a1...aN and an integer K) we will be able to identify (whether the solution is correct or not) certificate in polynomial time. This can be done by checking that the sum of the integers in subset S' is equal to K.
Subset Sum is NP-Hard:
In order to prove Subset Sum is NP-Hard, perform a reduction from a known NP-Hard problem to this problem.
Carry out a reduction from which the Vertex Cover Problem can be reduced to the Subset Sum problem. Let us assume a graph G(V, E) where V = {1, 2, ..., N}. Now, for every vertex i, ai=i. For every edge (i, j) we define a component called, bij.
We will represent the integers in a matrix format, where every row is expressed in the base-4 representation of the corresponding integer value of |E|+1 digits.
The matrix has the following properties:
- The first column contains an integer value 1 for ai and 0 for bij.
- Each of the E columns starting from the right side of the matrix represents a digit for each edge. The column (i, j)=1 for ai, aj and bij, otherwise, it is equal to 0.
- We define a constant k' such that,
k' = k(4^{|E|}) + \sum _{i=0}^{|E|-1}2(4^{i})
Now, the following propositions hold:
- Let us consider a subset of vertices and edges to (V', E') respectively, such that
\sum _{i\epsilon V'}a_{i} + \sum _{(i, j)\epsilon E'}b_{ij} = k'
- bij can contain at most 1 in every column. Also, the k' parameter has a 2 in all less significant digits up to |E|. We can never have a carry-in these digits. Now, these digits sum up to at-most three 1's in each column. This implies that for every edge (i, j), V' must contain either i or j. Therefore, V' becomes a vertex cover.
- Let us assume there is a Vertex Cover of size k, we will choose integers ai such that i lies in V' and all bij Such that either i or j is in V'. On summation of all these integers in base 4 representation(that we choose from the matrix), we get sum of integers =k'. Therefore, the chosen integers form the subset of integers with sum = k'. Therefore, subset sum holds.
Let us consider the following example,
Given is a vertex cover V = {1, 3} and k = 2

Now, a1 = 1, a2 = 2, a3 = 3, a4 = 4
The matrix can be constructed in the following way:

=> k' = k(4^{4})+\sum _{i=0}^{3}2(4^{i})
=> k' = 2(4^{4})+2(4^{0})+2(4^{1})+2(4^{2})+2(4^{3})
=> k' = 2(256+1+4+16+64) = 682
Now, to prove the value of k' let us choose ai such that i lies in V', we choose a1 and a3 and bij such that either i or j lies in V', that is we choose bij such that either i or j is in V', that is we choose b12, b14, b23 and b34 from the matrix . In base 4 representation, we have the following values:
a1 = 321, a3 = 276, b12 = 64, b23 = 16, b14 = 1, b34 = 4
These values are computed using the matrix. On summation of these values, we get,
k' = 321 + 276 + 64 + 16 + 1 + 4 = 682.
Hence, k' value can be calculated and verified.
Therefore, the Subset Sum Problem is NP-Complete.
Similar Reads
Subset Sum Problem Given an array arr[] of non-negative integers and a value sum, the task is to check if there is a subset of the given array whose sum is equal to the given sum. Examples: Input: arr[] = [3, 34, 4, 12, 5, 2], sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9.Input: arr[] = [3, 34, 4
15+ min read
Subset sum in Different languages
Python Program for Subset Sum Problem | DP-25Write a Python program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input:
7 min read
Java Program for Subset Sum Problem | DP-25Write a Java program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: se
8 min read
C Program for Subset Sum Problem | DP-25Write a C program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set[]
8 min read
PHP Program for Subset Sum Problem | DP-25Write a PHP program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set
7 min read
C# Program for Subset Sum Problem | DP-25Write a C# program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: set[
8 min read
Subset Sum Problem using Backtracking Given a set[] of non-negative integers and a value sum, the task is to print the subset of the given set whose sum is equal to the given sum. Examples:Â Input:Â set[] = {1,2,1}, sum = 3Output:Â [1,2],[2,1]Explanation:Â There are subsets [1,2],[2,1] with sum 3. Input:Â set[] = {3, 34, 4, 12, 5, 2}, sum =
8 min read
Print all subsets with given sum Given an array arr[] of non-negative integers and an integer target. The task is to print all subsets of the array whose sum is equal to the given target. Note: If no subset has a sum equal to target, print -1.Examples:Input: arr[] = [5, 2, 3, 10, 6, 8], target = 10Output: [ [5, 2, 3], [2, 8], [10]
15+ min read
Subset Sum Problem in O(sum) space Given an array of non-negative integers and a value sum, determine if there is a subset of the given set with sum equal to given sum. Examples: Input: arr[] = {4, 1, 10, 12, 5, 2}, sum = 9Output: TRUEExplanation: {4, 5} is a subset with sum 9. Input: arr[] = {1, 8, 2, 5}, sum = 4Output: FALSE Explan
13 min read
Subset Sum is NP Complete Prerequisite: NP-Completeness, Subset Sum Problem Subset Sum Problem: Given N non-negative integers a1...aN and a target sum K, the task is to decide if there is a subset having a sum equal to K. Explanation: An instance of the problem is an input specified to the problem. An instance of the subset
5 min read
Minimum Subset sum difference problem with Subset partitioning Given a set of N integers with up to 40 elements, the task is to partition the set into two subsets of equal size (or the closest possible), such that the difference between the sums of the subsets is minimized. If the size of the set is odd, one subset will have one more element than the other. If
13 min read
Maximum subset sum such that no two elements in set have same digit in them Given an array of N elements. Find the subset of elements which has maximum sum such that no two elements in the subset has common digit present in them.Examples: Input : array[] = {22, 132, 4, 45, 12, 223} Output : 268 Maximum Sum Subset will be = {45, 223} . All possible digits are present except
12 min read
Find all distinct subset (or subsequence) sums of an array Given an array arr[] of size n, the task is to find a distinct sum that can be generated from the subsets of the given sets and return them in increasing order. It is given that the sum of array elements is small.Examples: Input: arr[] = [1, 2]Output: [0, 1, 2, 3]Explanation: Four distinct sums can
15+ min read
Subset sum problem where Array sum is at most N Given an array arr[] of size N such that the sum of all the array elements does not exceed N, and array queries[] containing Q queries. For each query, the task is to find if there is a subset of the array whose sum is the same as queries[i]. Examples: Input: arr[] = {1, 0, 0, 0, 0, 2, 3}, queries[]
10 min read