Smallest Number That Cannot Be Represented as Sum of Subsets

Quick Overview

Given a sorted array of positive integers, find the smallest positive integer that no subset of the array sums to. The problem tests a linear greedy scan over reachable sums, the invariant that proves it correct, and overflow handling for large inputs.

Smallest Number That Cannot Be Represented as Sum of Subsets

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a sorted array of positive integers `nums`, find the smallest positive integer that cannot be represented as a sum of any subset of the given array. A subset can contain zero or more elements from the array. Each element can be used at most once. The empty subset has a sum of 0, so we are looking for the smallest positive integer that is not achievable. ### Function Signature ```python def find_smallest(nums: list[int]) -> int: ``` ### Rules - Equal values at different positions are separate elements, so `[1, 1]` can form 1 and 2. - The answer can be larger than the 32-bit integer range, since the total of `nums` can reach about `10^14`. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` - `nums` is sorted in non-decreasing order. ### Examples ```text Input: nums = [1, 2, 3, 8] Output: 7 ``` Using 1, 2 and 3 you can form every sum from 1 to 6 (1, 2, 3, 1+3=4, 2+3=5, 1+2+3=6). Any subset that includes 8 sums to at least 8, so 7 is the first value that cannot be formed. ```text Input: nums = [1, 1, 1, 1, 1] Output: 6 ``` Subsets of the five ones give every sum from 1 to 5, and no subset can exceed 5. ```text Input: nums = [1, 2, 5, 10] Output: 4 ``` The subsets of 1 and 2 give 1, 2 and 3. Every other element is at least 5, so no subset sums to exactly 4.

Overview: Given a sorted array of positive integers, find the smallest positive integer that no subset of the array sums to. The problem tests a linear greedy scan over reachable sums, the invariant that proves it correct, and overflow handling for large inputs.

Salesforce logo
Salesforce
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a sorted array of positive integers nums, find the smallest positive integer that cannot be represented as a sum of any subset of the given array.

A subset can contain zero or more elements from the array. Each element can be used at most once. The empty subset has a sum of 0, so we are looking for the smallest positive integer that is not achievable.

Function Signature

def find_smallest(nums: list[int]) -> int:

Rules

  • Equal values at different positions are separate elements, so [1, 1] can form 1 and 2.
  • The answer can be larger than the 32-bit integer range, since the total of nums can reach about 10^14 .

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9
  • nums is sorted in non-decreasing order.

Examples

Input: nums = [1, 2, 3, 8]
Output: 7

Using 1, 2 and 3 you can form every sum from 1 to 6 (1, 2, 3, 1+3=4, 2+3=5, 1+2+3=6). Any subset that includes 8 sums to at least 8, so 7 is the first value that cannot be formed.

Input: nums = [1, 1, 1, 1, 1]
Output: 6

Subsets of the five ones give every sum from 1 to 5, and no subset can exceed 5.

Input: nums = [1, 2, 5, 10]
Output: 4

The subsets of 1 and 2 give 1, 2 and 3. Every other element is at least 5, so no subset sums to exactly 4.