
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
Suppose we have a sorted array of positive numbers, this array is sorted in ascending order, er have to find the smallest positive value that cannot be represented as sum of elements of any subset of given set. We have to solve this problem in O(n) time.
So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2.
To solve this, we will follow these steps −
n := size of A
answer := 1
for i in range 0 to n, do
if A[i] <= answer, then
answer := answer + A[i]
otherwise,
come out from the loop
return answer
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 (0, n ): if A[i] <= answer: answer = answer + A[i] else: break return answer A = [1, 4, 8, 12, 13, 17] print(get_smallest_element(A))
Input
[1, 4, 8, 12, 13, 17]
Output
2
- Related Articles
- Smallest positive value that cannot be represented as sum of subarray JavaScript
- Show that the square of any positive integer cannot be of the form $6m+2$ or $6m+5$ for any positive integer $m$.
- Show that the square of any positive integer cannot be of the form $5q + 2$ or $5q + 3$ for any integer $q$.
- Show that the square of any positive integer cannot be of the form $6m+ 2$ or $6m + 5$ for any integer $m$.
- Show that the square of any positive integer cannot be of the form $3m+2$, where $m$ is a natural number.
- Find the sum of maximum difference possible from all subset of a given array in Python
- Array with GCD of any of its subset belongs to the given array?
- What is the sum of the largest 4-digit positive integer and the smallest 3-digit negative integer?
- Finding the smallest positive integer not present in an array in JavaScript
- Check if N can be represented as sum of integers chosen from set {A, B} in Python
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Program to check n can be represented as sum of k primes or not in Python
- Check if a given number can be represented in given a no. of digits in any base in C++
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Prove that the square of any positive integer is of the form $4q$ or $4q+1$ for some integer q.

Advertisements