Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
In some programming problems, we need to find the smallest positive integer that cannot be represented as the sum of any subset from a given sorted array. This is a classic greedy algorithm problem that can be solved efficiently in O(n) time complexity.
So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2, because we can represent 1 using [1], but we cannot represent 2 using any subset combination.
Algorithm Approach
The key insight is that if we can represent all numbers from 1 to answer-1, and the current element A[i] <= answer, then we can represent all numbers from 1 to answer + A[i] - 1.
To solve this, we will follow these steps −
Initialize
answer = 1(smallest positive integer we're looking for)Iterate through each element in the sorted array
If current element
A[i] <= answer, then we can extend our range by addingA[i]toanswerIf
A[i] > answer, we found our answer since there's a gap in representationReturn the final
answervalue
Example
Let us see the following implementation to get better understanding −
def get_smallest_element(A):
n = len(A)
answer = 1
for i in range(n):
if A[i] <= answer:
answer = answer + A[i]
else:
break
return answer
# Test the function
A = [1, 4, 8, 12, 13, 17]
result = get_smallest_element(A)
print(f"Array: {A}")
print(f"Smallest unrepresentable positive integer: {result}")
Array: [1, 4, 8, 12, 13, 17] Smallest unrepresentable positive integer: 2
How It Works
Let's trace through the algorithm step by step ?
def get_smallest_element_with_trace(A):
n = len(A)
answer = 1
print(f"Initial answer: {answer}")
for i in range(n):
print(f"Checking A[{i}] = {A[i]} against answer = {answer}")
if A[i] <= answer:
answer = answer + A[i]
print(f" A[{i}] <= {answer - A[i]}, so answer becomes {answer}")
else:
print(f" A[{i}] > {answer}, breaking loop")
break
return answer
# Trace through the example
A = [1, 4, 8, 12, 13, 17]
result = get_smallest_element_with_trace(A)
print(f"Final result: {result}")
Initial answer: 1 Checking A[0] = 1 against answer = 1 A[0] <= 1, so answer becomes 2 Checking A[1] = 4 against answer = 2 A[1] > 2, breaking loop Final result: 2
Additional Examples
Let's test with different arrays to understand the pattern better ?
def test_multiple_cases():
test_cases = [
[1, 3, 6, 10, 11, 15],
[1, 1, 1, 1],
[1, 2, 3, 4, 5],
[2, 3, 4, 5]
]
for i, arr in enumerate(test_cases, 1):
result = get_smallest_element(arr)
print(f"Test {i}: {arr} ? {result}")
test_multiple_cases()
Test 1: [1, 3, 6, 10, 11, 15] ? 7 Test 2: [1, 1, 1, 1] ? 5 Test 3: [1, 2, 3, 4, 5] ? 16 Test 4: [2, 3, 4, 5] ? 1
Time and Space Complexity
Time Complexity: O(n) where n is the length of the array, as we iterate through each element once
Space Complexity: O(1) as we only use a constant amount of extra space
Conclusion
This greedy algorithm efficiently finds the smallest positive integer that cannot be represented as a subset sum. The key insight is tracking the range of representable numbers and extending it when possible, stopping when we encounter a gap.
