Not a subset sum (original) (raw)

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 Yourselfredirect icon

Table of Content

[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}

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

C++ `

#include #include using namespace std;

int findSmallest(vector &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 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));

`

[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}

Final answer : 7

C++ `

#include #include using namespace std;

int findSmallest(vector &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 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));

`