Open In App

Smallest Value that cannot be represented as sum of any subset of a given array

Last Updated : 04 May, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

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:

  1. Calculate the total sum of all array elements and create a boolean array to track possible sums.
  2. Mark sum 0 as possible (true) since it can be achieved with an empty subset.
  3. For each element in the array, update possible sums in reverse order to avoid counting an element multiple times.
  4. Each time we encounter a possible sum, mark that sum plus current element as also possible.
  5. Scan through the boolean array to find the first impossible sum, or return total sum + 1 if all sums are possible.
C++
Java Python C# JavaScript

Output
2

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

  1. 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’.
  2. 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:

  1. Sort the array in ascending order to process elements from smallest to largest.
  2. Maintain a running sum that represents the smallest number we cannot yet form.
  3. 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++
Java Python C# JavaScript

Output
2

Next Article
Article Tags :
Practice Tags :

Similar Reads