smallest number
array sums
mathematics
number theory
problem solving

Smallest number that cannot be formed from sum of numbers from array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

The concept of finding the smallest number that cannot be formed using the sum of any subset from a given array is a classic problem often encountered in algorithm design and number theory. This intriguing problem not only has practical applications in areas such as computing and cryptography but also serves as an excellent exercise in understanding and applying basic principles of combinatorics and mathematics.

Understanding the Problem

Given an array of positive integers, the challenge is to determine the smallest positive integer that cannot be represented as the sum of any subset of the provided array. This problem is generally tackled using a greedy algorithm, which is efficient given that it allows us to progressively build sums in a systematic manner without the need for generating all possible subsets explicitly.

Example

Consider the array `[1, 2, 5, 10]`. Here’s how we would analyze it:

  1. We start with the number `1`. Since we have `1` in our array, any number greater than or equal to `1` can potentially be formed.
  2. Next, consider adding `2`. Since `1` or `2` can be formed, we now try for `3` (i.e., `1 + 2`).
  3. With `5` included, we can form additional sums: `1 + 5 = 6`, `2 + 5 = 7`, and `1 + 2 + 5 = 8`. Now, we can form any number between `1` and `8`.
  4. Finally, adding `10` extends our range of possible sums beyond `8`.

Thus, every number up to the highest sum of these integers can be formed. Here, however, there might be gaps, and the smallest positive integer that cannot be formed is what we are interested in finding.

Greedy Approach in Detail

For a sorted array, the smallest unconstructible sum can be efficiently found using a greedy algorithm. The crux of this approach leverages the fact that if we can construct all sums up to a certain number, then we either use the next array element to extend our range or we find the gap. Following is the detailed process:

  1. Sort the Array: Begin by sorting the array of positive integers.
  2. Initialize Variables: Start with a variable (let's call it `smallest`) initialized to `1`. This variable represents the smallest sum that we aren't able to currently form.
  3. Iterate Through the Array: For each number in the array:
    • If the current number is greater than `smallest`, then `smallest` is the smallest number that cannot be formed.
    • Otherwise, add the number to `smallest` to extend the range of possible sums.
  4. End: After iterating through the array, `smallest` will hold the desired smallest number that cannot be formed.

Example Walkthrough

Consider using the array `[1, 3, 4, 7]`:

  • Start with `smallest = 1`.
  • For `1`, since `1 <= 1`, update `smallest` to `1 + 1 = 2`.
  • For `3`, since `3 > 2`, it indicates that `2` cannot be formed.

Hence, the smallest number that cannot be the sum of a subset of `[1, 3, 4, 7]` is `2`.

Key Points Summarization

To efficiently summarize the salient points of this method, observe the following table:

StepAction
Sort ArrayArrange the numbers in non-decreasing order.
Initialize SmallestStart with smallest = 1
Iterate and Update SmallestFor each element, if it's greater than smallest, terminate. Otherwise, add the element to smallest.
ResultThe last value of smallest before termination is the smallest unformable sum.

Applications and Further Reading

This problem extends beyond academic intrigue into various applications:

  • Security and Cryptography: Problems similar to subset formation are critical in understanding the limitations and boundaries of cryptographic constructs.
  • Computational Efficiency: Analyzing sums and finding gaps are processes used in many algorithmic solutions where resource constraints are a factor.

For those interested in delving deeper, examining coin change problems or exploring related algorithmic strategies can provide further insights into the complexity and optimization involved in handling ordered numeric arrays.

In summary, the problem of finding the smallest number unformable by the sum of array subsets reveals fascinating aspects of number manipulation and algorithmic design. Through a combination of sorting and iterative checks, the process elegantly converges to a result, showcasing a perfect balance between computational simplicity and theoretical depth.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms