<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Dexter&#39;s Lab - Medium]]></title>
        <description><![CDATA[Tech, Travel and more - Medium]]></description>
        <link>https://medium.com/dexters-lab?source=rss----8e697b997565---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Dexter&amp;#39;s Lab - Medium</title>
            <link>https://medium.com/dexters-lab?source=rss----8e697b997565---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 00:59:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/dexters-lab" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ELI5: Find the smallest positive integer value that cannot be represented as sum of any subset of…]]></title>
            <link>https://medium.com/dexters-lab/eli5-find-the-smallest-positive-integer-value-that-cannot-be-represented-as-sum-of-any-subset-of-f8ea2488184b?source=rss----8e697b997565---4</link>
            <guid isPermaLink="false">https://medium.com/p/f8ea2488184b</guid>
            <category><![CDATA[eli5]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Dexter]]></dc:creator>
            <pubDate>Wed, 17 Jul 2019 13:33:09 GMT</pubDate>
            <atom:updated>2019-07-20T12:35:10.903Z</atom:updated>
            <content:encoded><![CDATA[<h3>ELI5: Find the smallest positive integer value that cannot be represented as sum of any subset of a given array</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pbstxyrLiVlzvfzSHIsK3Q.jpeg" /></figure><p>This is a problem with a tricky solution and I scoured the internet for a decent explanation of why the solution works but couldn’t find any. So I conferred with a friend over the solution and decided to share ‘why and how’ the solution works. <br>This is what I would like to call the “<strong>ELI5: Explain Like I’m 5”</strong> series.</p><p><strong>Q: </strong>Given an array (sorted in non-decreasing order) of positive numbers, find the smallest positive integer value that cannot be represented as sum of elements of any subset of the given set. <br>Expected time complexity is O(n).</p><p>Original Question: <a href="https://www.geeksforgeeks.org/find-smallest-value-represented-sum-subset-given-array/">GeeksForGeeks</a> and <a href="https://www.careercup.com/question?id=5638615906385920">CareerCup</a></p><p>The question can be posed slightly differently where in the given array might not be sorted to begin with, then we would have to sort it on our own in O(nlogn) time. For now, we will consider that we are operating on a sorted array.</p><h4><strong>Brute Force:</strong></h4><p>The question kinda gives away the brute force approach by suggesting subsets.</p><ol><li>You can generate all possible 2^n subsets (n — number of elements in the array)</li><li>Compute the sum of each subset,</li><li>Insert those sums into a <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.add?view=netframework-4.8">HashSet</a> and then</li><li>Run a loop i: 1 → infinity to check if ‘i’ is present in the hashSet. The first element that is missing from the hashSet is our answer.</li></ol><p>Generating subsets is an <a href="https://www.youtube.com/watch?v=YX40hbAHx3s">NP-Complete</a> problem and so there is no polynomial time solution for it (yet). In fact, the time complexity of this approach: O(2^n)</p><h4><strong>Optimized approach:</strong></h4><p>Now let’s try to solve it in O(n). Consider the following sorted array ‘a’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5NQbL5A5jcfVhE20hPA8hg.png" /></figure><p>Let ‘maxPossible’ be a running variable which indicates the maximum possible number we can create by adding up any amount of numbers in the array. Our final answer would be ‘maxPossible+1’ as that would be the number which cannot be formed from the elements of the array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/beed555681b72e23e8d32d4953cc497a/href">https://medium.com/media/beed555681b72e23e8d32d4953cc497a/href</a></iframe><p>Since the question asks us to find the smallest positive integer, we can check whether ‘1’ is present in the array or not at position a[0]. If ‘1’ is missing, then we can immediately say we found our answer and return ‘1’. But if ‘1&#39; is indeed present at a[0], then we can safely initialize ‘maxPossible’ to ‘1’ as we can now at-least create ‘1&#39; from the array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b7b18d47f5d4a6791c5b9f5e8b78a85/href">https://medium.com/media/3b7b18d47f5d4a6791c5b9f5e8b78a85/href</a></iframe><p>Let’s move on to a[1] which is also ‘1’. So with these two ones, we can create 1 &amp; 2, so our maxPossible at this point is 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AFp-RZkTcB2np19l1LIubA.png" /></figure><p>Now comes the interesting part and this is where the algorithm takes off. We have two numbers [1, 2] which can be created and we have a new number [3] at position a[2] being added into the mix. So that means, now we can create 5 consecutive positive integers:</p><ul><li>[1, 2] that we already had</li><li>[3] the new entrant solo entry</li><li>[1, 2] + [3] = [4, 5] — new entrant being added to all the previous numbers</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JbfE4Sni8RNYYBZIkmcKKw.png" /></figure><h4>The Key:</h4><p>A positive integer ‘k’ being added to a series of consecutive positive integers [1, 2, 3, …….. p] furthers that series by K elements without disrupting the continuity as long as k ≤ (p+1). This is just a formal way of stating what we observed in the diagram above where p = 2, k = 3 and the series now has 5 elements. Understanding and internalizing this property is the key to grokking this algorithm.</p><p>We have processed till a[2] and maxPossible is now ‘5’. If the next element in the input array a[3] were &gt; 6 (say 7), then we would have our answer because then from [1, 2, 3, 4, 5] and [7]there’s no way to produce ‘6’.</p><p>But a[3] is 5 which is ≤ (maxPossible + 1), so we can still continue extending our string of consecutive integers to maxPossible+newEntrant = 5+5 = 10. And that makes maxPossible = 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SD_SPN-oJDjitfnDvlADng.png" /></figure><p>We now check if the next element: a[4] ≤ (maxPossible + 1) (i.e. 8 &lt; 11). Since this condition is satisfied, the sequence can be furthered to: maxPossible + a[4] = 10 + 8 = 18 which becomes the new maxPossible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TClvSoBmBZTAVxiGFwI7CA.png" /></figure><p>Now when we looking for a value ≤ 19 (18+1) in a[5], we hit a roadblock (or a jackpot, depends on how you see it) in that we find 21, leaving a gap of two numbers [19,20] which can’t be formed from [1,2,…..,18] + [21] (Remember the key property we discussed)</p><p>So that’s our answer: 19 (maxPossible + 1)</p><p>Here’s the code snippet to do that and hopefully it all makes sense now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ff4a4d260baecd658848c770942ec82/href">https://medium.com/media/6ff4a4d260baecd658848c770942ec82/href</a></iframe><p><em>If you enjoyed this post, please share, comment, and press that 👏 a few times (up to 50 times). . . Maybe it will help someone.</em></p><p><strong>PS: </strong>I’m thinking of carrying on this ELI5 series and if any of you have an algorithm/programming problem/CS concept for which you are struggling to find a decent explanation online, please leave a comment below and I will try to explain it in simpler terms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8ea2488184b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dexters-lab/eli5-find-the-smallest-positive-integer-value-that-cannot-be-represented-as-sum-of-any-subset-of-f8ea2488184b">ELI5: Find the smallest positive integer value that cannot be represented as sum of any subset of…</a> was originally published in <a href="https://medium.com/dexters-lab">Dexter&#39;s Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>