Not a subset sum

Last Updated : 5 May, 2026

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
Explanation: 2 is the smallest positive number for which no subset is there with sum 2.

Input: arr[] = {1, 1, 1, 1};
Output: 5
Explanation: 5 is the smallest positive number for which no subset is there with sum 5.

Input: arr[] = {1, 1, 3, 4};
Output: 10
Explanation: 10 is the smallest positive number for which no subset is there with sum 10.

Try It Yourself
redirect icon

[Naive Approach] Using Dynamic Programming - O(n * sum) time and O(sum) space

The solution uses dynamic programming similar to the Subset Sum problem. First, compute the total sum of the array and create a boolean DP array where dp[s] indicates whether sum s can be formed. Initialize dp[0] = true (empty subset). For each element, update the DP array to mark new reachable sums. Finally, scan the DP array to find the smallest index with false, which is the smallest sum that cannot be formed.

consider the following dry run: arr = {1, 2, 3}

  • Find sum of all elements: sum = 1 + 2 + 3 = 6
  • dp[x] = true means we can form sum x using some subset.
  • Initially, dp[0] = true (empty subset always gives sum 0)
  • For i = 0 (val = 1) : dp[0] = true --> dp[1] = true (using 1, we can now form sum 1).
  • For i = 1 (val = 2) : dp[1] = true --> dp[3] = true (1+2)
    dp[0] = true --> dp[2] = true (0 + 2)
  • For i = 2 (val = 3) : dp[3] = true --> dp[6] = T (3 + 3)
    dp[2] = true --> dp[5] = true (2 + 3)
    dp[1] = true --> dp[4] = true (1 + 3)
    dp[0] = true --> dp[3] = true (just 3)

Now we can form: {0,1,2,3,4,5,6} so final answer : 7

C++
#include <iostream>
#include <vector>
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
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
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

# Driver code
if __name__ == "__main__":
    arr = [1, 10, 3, 11, 6, 15]
    print(findSmallest(arr))
C#
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
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;
}
// Driver code
let arr = [1, 10, 3, 11, 6, 15];
console.log(findSmallest(arr));

Output
2

[Expected Approach] Using Sorting - O(n Log n) time and O(1) space

The idea is to sort the array and maintain the smallest sum res that cannot be formed so far. Initialize res = 1, meaning we cannot form 1 initially. Traverse the sorted array: if the current element arr[i] is greater than res, then res is the smallest missing sum. Otherwise, include arr[i] and extend the reachable range by updating res += arr[i]. This works because if we can form all sums from 1 to res−1, adding arr[i] lets us form sums up to res + arr[i] − 1.

Consider the following dry run: arr = {1, 3, 2}

  • Sort the array : sorted = {1, 2, 3}
  • Initially, res = 1 (we cannot form 1 yet).
  • For i = 0 (val = 1) : 1 ≤ res(1) --> include --> now we can form [1] --> update res = 2
  • For i = 1 (val = 2) : 2 ≤ res(2) --> include --> now we can form [1, 2, 3] --> update res = 4
  • For i = 2 (val = 3) : 3 ≤ res(4) --> include --> now we can form [1, 2, 3, 4, 5, 6] -->update res = 7
  • Final State : We can form all sums from 1 to 6.

Final answer : 7

C++
#include <iostream>
#include <vector>
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
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
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

# Driver code
if __name__ == "__main__":
    arr = [1, 10, 3, 11, 6, 15]
    print(findSmallest(arr))
C#
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
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;
}
//Driver code
let arr = [1, 10, 3, 11, 6, 15];
console.log(findSmallest(arr));

Output
2
Comment