Smallest Value that cannot be represented as sum of any subset of a given array
Last Updated :
04 May, 2025
Given an array of positive numbers, the task is to find the smallest positive integer value that cannot be represented as the sum of elements of any subset of a given set.
Examples:
Input: arr[] = {1, 10, 3, 11, 6, 15};
Output: 2
Input: arr[] = {1, 1, 1, 1};
Output: 5
Input: arr[] = {1, 1, 3, 4};
Output: 10
[Naive Approach] Using Dynamic Programming - O(n * sum) time and O(sum) space
The idea is based on dynamic programming solution of Subset Sum Problem. We track all possible sums that can be created from the given array. We first calculate the total sum of all elements, create a boolean DP array of that size, and mark index 0 as true (since an empty subset has sum 0).
Then, for each array element, we update the DP array to mark all possible sums that can be achieved by including that element. Finally, we scan the DP array to find the smallest index that is false, which represents the smallest sum that cannot be formed.
Step by step approach:
- Calculate the total sum of all array elements and create a boolean array to track possible sums.
- Mark sum 0 as possible (true) since it can be achieved with an empty subset.
- For each element in the array, update possible sums in reverse order to avoid counting an element multiple times.
- Each time we encounter a possible sum, mark that sum plus current element as also possible.
- Scan through the boolean array to find the first impossible sum, or return total sum + 1 if all sums are possible.
C++
// C++ program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
#include <bits/stdc++.h>
using namespace std;
int findSmallest(vector<int> &arr) {
// Find total sum of array values
int sum = 0;
for (auto val: arr) sum += val;
vector<bool> dp(sum+1, false);
// Sum 0 is always possible due to
// empty subset.
dp[0] = true;
// For each value in the array
for (int i=0; i<arr.size(); i++) {
for (int j=sum-arr[i]; j>=0; j--) {
// If sum j is possible, then j + arr[i]
// is also possible.
if (dp[j]) dp[j+arr[i]] = 1;
}
}
// Find the minimum value for which
// sum is not possible.
for (int i=0; i<=sum; i++) {
if (dp[i]==false) return i;
}
// If all values are possible, then
// return sum + 1.
return sum +1;
}
int main() {
vector<int> arr = {1, 10, 3, 11, 6, 15};
cout << findSmallest(arr);
return 0;
}
Java
// Java program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
class GfG {
static int findSmallest(int[] arr) {
// Find total sum of array values
int sum = 0;
for (int val : arr) sum += val;
boolean[] dp = new boolean[sum + 1];
// Sum 0 is always possible due to
// empty subset.
dp[0] = true;
// For each value in the array
for (int i = 0; i < arr.length; i++) {
for (int j = sum - arr[i]; j >= 0; j--) {
// If sum j is possible, then j + arr[i]
// is also possible.
if (dp[j]) dp[j + arr[i]] = true;
}
}
// Find the minimum value for which
// sum is not possible.
for (int i = 0; i <= sum; i++) {
if (!dp[i]) return i;
}
// If all values are possible, then
// return sum + 1.
return sum + 1;
}
public static void main(String[] args) {
int[] arr = {1, 10, 3, 11, 6, 15};
System.out.println(findSmallest(arr));
}
}
Python
# Python program to Find the smallest positive integer value that
# cannot be represented as sum of any subset of a given array
def findSmallest(arr):
# Find total sum of array values
sum = 0
for val in arr:
sum += val
dp = [False] * (sum + 1)
# Sum 0 is always possible due to
# empty subset.
dp[0] = True
# For each value in the array
for i in range(len(arr)):
for j in range(sum - arr[i], -1, -1):
# If sum j is possible, then j + arr[i]
# is also possible.
if dp[j]:
dp[j + arr[i]] = True
# Find the minimum value for which
# sum is not possible.
for i in range(sum + 1):
if not dp[i]:
return i
# If all values are possible, then
# return sum + 1.
return sum + 1
if __name__ == "__main__":
arr = [1, 10, 3, 11, 6, 15]
print(findSmallest(arr))
C#
// C# program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
using System;
class GfG {
static int findSmallest(int[] arr) {
// Find total sum of array values
int sum = 0;
foreach (int val in arr) sum += val;
bool[] dp = new bool[sum + 1];
// Sum 0 is always possible due to
// empty subset.
dp[0] = true;
// For each value in the array
for (int i = 0; i < arr.Length; i++) {
for (int j = sum - arr[i]; j >= 0; j--) {
// If sum j is possible, then j + arr[i]
// is also possible.
if (dp[j]) dp[j + arr[i]] = true;
}
}
// Find the minimum value for which
// sum is not possible.
for (int i = 0; i <= sum; i++) {
if (!dp[i]) return i;
}
// If all values are possible, then
// return sum + 1.
return sum + 1;
}
static void Main(string[] args) {
int[] arr = {1, 10, 3, 11, 6, 15};
Console.WriteLine(findSmallest(arr));
}
}
JavaScript
// JavaScript program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
function findSmallest(arr) {
// Find total sum of array values
let sum = 0;
for (let val of arr) sum += val;
let dp = Array(sum + 1).fill(false);
// Sum 0 is always possible due to
// empty subset.
dp[0] = true;
// For each value in the array
for (let i = 0; i < arr.length; i++) {
for (let j = sum - arr[i]; j >= 0; j--) {
// If sum j is possible, then j + arr[i]
// is also possible.
if (dp[j]) dp[j + arr[i]] = true;
}
}
// Find the minimum value for which
// sum is not possible.
for (let i = 0; i <= sum; i++) {
if (!dp[i]) return i;
}
// If all values are possible, then
// return sum + 1.
return sum + 1;
}
let arr = [1, 10, 3, 11, 6, 15];
console.log(findSmallest(arr));
[Expected Approach] Using Sorting - O(n Log n) time and O(1) space
The idea is to sort the array in ascending order. After Sorting, we can solve this problem using a simple loop. Let the input array be arr[0..n-1]. We first sort the array in non-decreasing order. Let the smallest element that cannot be represented by elements at indexes from 0 to (i-1) be ‘res’. We initialize ‘res’ as 1 (smallest possible answer) and traverse the given array from i=0. There are the following two possibilities when we consider element at index i:
- We decide that ‘res’ is the final result: If arr[i] is greater than ‘res’, then we found the gap which is ‘res’ because the elements after arr[i] are also going to be greater than ‘res’.
- The value of ‘res’ is incremented after considering arr[i]: If arr[i] is not greater than ‘res’, the value of ‘res’ is incremented by arr[i] so ‘res’ = ‘res’+arr[i] (why? If elements from 0 to (i-1) can represent 1 to ‘res-1’, then elements from 0 to i can represent from 1 to ‘res + arr[i] – 1’ by adding arr[i] to all subsets that represent 1 to ‘res-1’ which we have already determined to be represented)
Let arr = [1, 10, 2, 11, 6, 15]
After Sorting, we get, arr = [1, 2, 6, 10, 11, 15]
We initialize res = 1. Since 1 <= res, we make res = 1 + 1 = 2
Since 2 <= res, res = 2 + 2 = 4
Since 6 is not <= res, we return res.
Step by step approach:
- Sort the array in ascending order to process elements from smallest to largest.
- Maintain a running sum that represents the smallest number we cannot yet form.
- For each element, if it's less than or equal to our running sum, add it to the running sum; otherwise we've found our answer.
C++
// C++ program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
#include <bits/stdc++.h>
using namespace std;
int findSmallest(vector<int> &arr) {
int res = 1;
// Sort the array
sort(arr.begin(), arr.end());
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for (int i=0; i<arr.size() && arr[i] <= res; i++) {
res += arr[i];
}
return res;
}
int main() {
vector<int> arr = {1, 10, 3, 11, 6, 15};
cout << findSmallest(arr);
return 0;
}
Java
// Java program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
import java.util.Arrays;
class GfG {
static int findSmallest(int[] arr) {
int res = 1;
// Sort the array
Arrays.sort(arr);
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for (int i = 0; i < arr.length && arr[i] <= res; i++) {
res += arr[i];
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 10, 3, 11, 6, 15};
System.out.println(findSmallest(arr));
}
}
Python
# Python program to Find the smallest positive integer value that
# cannot be represented as sum of any subset of a given array
def findSmallest(arr):
res = 1
# Sort the array
arr.sort()
# Traverse the array and increment 'res' if arr[i] is
# smaller than or equal to 'res'.
for i in range(len(arr)):
if arr[i] <= res:
res += arr[i]
else:
break
return res
if __name__ == "__main__":
arr = [1, 10, 3, 11, 6, 15]
print(findSmallest(arr))
C#
// C# program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
using System;
class GfG {
static int findSmallest(int[] arr) {
int res = 1;
// Sort the array
Array.Sort(arr);
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for (int i = 0; i < arr.Length && arr[i] <= res; i++) {
res += arr[i];
}
return res;
}
static void Main(string[] args) {
int[] arr = {1, 10, 3, 11, 6, 15};
Console.WriteLine(findSmallest(arr));
}
}
JavaScript
// JavaScript program to Find the smallest positive integer value that
// cannot be represented as sum of any subset of a given array
function findSmallest(arr) {
let res = 1;
// Sort the array
arr.sort((a, b) => a - b);
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for (let i = 0; i < arr.length && arr[i] <= res; i++) {
res += arr[i];
}
return res;
}
let arr = [1, 10, 3, 11, 6, 15];
console.log(findSmallest(arr));
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 = 30
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