VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-smallest-value-represented-sum-subset-given-array/

⇱ Not a subset sum - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

[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


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


Output
2
Comment
Article Tags:
Article Tags: