- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- 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?
- Python Program to Find the Smallest Divisor of an 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
- 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++
- Returning the value of (count of positive / sum of negatives) for an array in JavaScript
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N 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
- Count numbers which can be represented as sum of same parity primes in C++
- Find a triplet that sum to a given value in C++

Advertisements