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
Like Article
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++
// 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));

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++
// 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));

Output
2

Next Article
Article Tags :
Practice Tags :

Similar Reads