- Related Questions & Answers
- Smallest positive value that cannot be represented as sum of subarray JavaScript
- 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?
- Check if a given number can be represented in given a no. of digits in any base in C++
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Find Positive Integer Solution for a Given Equation in C++
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Count numbers which can be represented as sum of same parity primes 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
- Sum of two numbers where one number is represented as array of digits in C++
- Find the sum of array in Python Program
- Find frequency of smallest value in an array in C++
- Find the minimum sum of distance to A and B from any integer point in a ring of size N in Python
- Python Program to find the sum of array

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

[1, 4, 8, 12, 13, 17]

2

Advertisements