<?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[Stories by Vardan Grigoryan on Medium]]></title>
        <description><![CDATA[Stories by Vardan Grigoryan on Medium]]></description>
        <link>https://medium.com/@vardannix?source=rss-1e0249bea094------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4b3yj0-XsSji-f9b-k7Mgw.png</url>
            <title>Stories by Vardan Grigoryan on Medium</title>
            <link>https://medium.com/@vardannix?source=rss-1e0249bea094------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 23:24:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vardannix/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Binary Search Over a Binary Indexed Tree]]></title>
            <link>https://medium.com/swlh/binary-search-over-a-binary-indexed-tree-2d4046d81b51?source=rss-1e0249bea094------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d4046d81b51</guid>
            <category><![CDATA[binary-indexed-tree]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[time-complexity]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[prefix-sum]]></category>
            <dc:creator><![CDATA[Vardan Grigoryan]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 15:31:49 GMT</pubDate>
            <atom:updated>2020-09-21T11:35:17.613Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I came across an interesting problem that could be solved by applying different data structures and algorithmic techniques. And here is the problem.</p><blockquote>Special integer, K, of an array, is an integer such that none of the subarrays of the original array of size K has sum of elements greater than X. The problem is to find the maximum value of the special integer.</blockquote><p>Input Format: The first line consists of two space-separated integers denoting N and X. Second-line consists of N space-separated integers denoting the array A.</p><p>Output Format: Print the maximum possible value of a special integer.</p><p><strong>Sample input</strong>:</p><p>4 8</p><p>1 2 3 4</p><p><strong>Sample output:</strong></p><p>2</p><p><strong>This problem could be solved by using :</strong></p><ol><li>prefix sum array,</li><li>segment tree</li><li>binary indexed tree</li></ol><p>And even a dynamic programming approach could be applied but it will require more time and space complexity.</p><p>Now let’s consider the following array:</p><blockquote><strong>array: {9, 3, 7, 8, 1} and X = 26</strong></blockquote><p>Having the input given above we can form the following subarrays:</p><blockquote><em>{9}; {3}; {7}; {8}; {1};</em></blockquote><blockquote><em>{9, 3}; {3, 7}; {7, 8}; {8, 1};</em></blockquote><blockquote><em>{9, 3, 7}; {3, 7, 8}; {7, 8, 1};</em></blockquote><blockquote><em>{9, 3, 7, 8}; {3, 7, 8, 1};</em></blockquote><blockquote><em>{9, 3, 7, 8, 1}</em></blockquote><p>Now let&#39;s calculate the sum for each subarray:</p><p><em>Original array:</em></p><blockquote><em>9, 3, 7, 8, 1</em></blockquote><p>Subarray sums:</p><blockquote><em>12, 10, 15, 9</em></blockquote><blockquote><em>19, 18, 16</em></blockquote><blockquote><em>27, 19</em></blockquote><blockquote><em>28</em></blockquote><blockquote>So, now it’s obvious that for the given X = 26 the subarray with maximum sum which is still less than X is sum {9, 3, 7} = 19.</blockquote><p>Every time when there is even the slightest hint of finding range sums or performing such a query the best association is <strong>segment tree. </strong>Segment tree is a perfect data structure for performing range queries. It could be constructed in O(N) time and the query and update operations will take O(logN) time. But there is another data structure that is even faster than the segment tree and consumes less memory, which is the Binary indexed tree(BIT) or Fenwick tree. BIT also requires O(N) time for construction and O(logN) for update and query but generally, it outperforms the segment tree with having less constant factors for the mentioned operations.</p><p>So for this problem, I’m choosing BIT as the main data structure to query the sums of the subarrays.</p><p>Before going ahead with BIT solution I would like to mention that this problem could be solved easily by using a simple prefix sum array. The prefix sum array can be constructed in O(N) time. The query operation will take O(1) time and because of this, it is even faster than BIT based solution.</p><p>For sake of clarity let’s construct the prefix sum array for the above-given array:</p><p>So the input array is:</p><blockquote>{9, 3, 7, 8, 1}</blockquote><p>and the appropriate prefix sum array will be:</p><blockquote>{9, 12, 19, 27, 28} (1)</blockquote><p>This is a simple array and any element can be taken by O(1) time and later on, we will see why this is important (**).</p><p>Now Let’s construct out BIT. In general, BIT can be chosen for its simplicity and the time which is required to code it.</p><p>Here is the simple code for BIT, all that we need to implement is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb6140a22675507ea9301669643f46a9/href">https://medium.com/media/eb6140a22675507ea9301669643f46a9/href</a></iframe><p>Now, let’s think how can we find the subarray with the required sum?</p><p>So, basically we are keeping the subarray sums in an ordered manner. In the case of prefix sum array, it is just a sorted array, and in the case of BIT each next query will give us a greater(or equal in case of zeros in the original array) value than for the previous ranges assuming a query for <strong><em>0 … i</em></strong> range. So this means that a binary search could be applied to find the required value.</p><blockquote>But how even to use it when we are not looking for the one particular value in BIT or prefix array but rather the sum difference of some elements?</blockquote><p>It is much easier to describe the logic of the binary search by considering the prefix sum array, so let’s consider our prefix sum array given in (1).</p><blockquote>{9, 12, 19, 27, 28}</blockquote><p>Let’s take its middle element which is 19 and our X which is 26. What that means is the sum of the first three elements(19) is less than our X(26), but we know nothing about the difference of subarray sums around the middle element. For example,</p><blockquote>array[3] — array[1] = 27 — 12 = 15;</blockquote><blockquote>array[4] — array[2] = 28 — 19 = 9;</blockquote><blockquote>So, what does this mean is that we have scanned the middle element value, and all of the differences of subarray sums around it and we haven’t found a single value greater than our X(12 &lt; 26, 9 &lt; 26) means that there are an elements which sums up to value less than X. And because of prefix sum is not a decreasing function (each next value is not less than the previous) we can increement the left pointer by <strong>mid + 1</strong>, binary search for the next middle element, compare its value with X and compare all of the differences of subarray sums with X around that middle element. In contrary when there is a sum difference greater than X we will decreement the right pointer by <strong>mid -1</strong> and continue the search untill <strong>left ≤ right</strong>.</blockquote><p>Keeping this logic in mind we can implement the following function for binary search over BIT:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c466f06cb2546f132a77af2ddc93fa95/href">https://medium.com/media/c466f06cb2546f132a77af2ddc93fa95/href</a></iframe><p>Now the most interesting part is the analysis of the complexity of the algorithm given above.</p><p>The first while loop takes O(logN) time.</p><p>The complicated part starts with the second loop. In fact, what we are doing in each iteration is scanning the range</p><blockquote>bit.size() — m</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*DaIGhFoZHsg5TQcDHcmHgg.jpeg" /></figure><p>Let’s look at the picture shown above.</p><blockquote>During the first iteration, we are doing n— m0 &lt;=&gt; n — n/2 operations. During the second iteration, we are doing n — m1 &lt; =&gt; n — n/4 operations and so on. During the kth iteration, we are performing n — n/2^k operations means we can write the following expression for the total T(N).</blockquote><p>(I&#39;ve used <a href="http://www.imatheq.com/imatheq/com/imatheq/math-equation-editor.html"><em>imatheq.com</em></a><em> </em>for writing math expressions)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZYMRyS28BSC7xMvZmNE-mg.png" /></figure><p>But wait, the query function takes O(logN) for BIT means we will have overall O(NlogN) complexity for the second loop.</p><blockquote>And finally, for the whole algorithm, we will have O(log N) * O(NlogN) which is:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/1*YL8rLoZqKdVupXPR09JNjg.png" /></figure><blockquote>Now lets come back to the prefix sum array. If we will choose to use prefix sum array instead of BIT we will save additional O(log N) time for acquiring elements and the total complexity will be:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*_bOYjk1NJGC3P979y1k9EA.png" /></figure><p>The code with the stress test is as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d53de82788efdf2d0e1a925351f81459/href">https://medium.com/media/d53de82788efdf2d0e1a925351f81459/href</a></iframe><p><strong>References:</strong></p><ol><li><a href="https://www.hackerearth.com/practice/notes/segment-tree-and-lazy-propagation/">Segment tree</a></li><li><a href="https://www.topcoder.com/community/competitive-programming/tutorials/binary-indexed-trees/">Binary indexed tree</a></li><li><a href="https://cp-algorithms.com/">CP-Algorithms</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d4046d81b51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/binary-search-over-a-binary-indexed-tree-2d4046d81b51">Binary Search Over a Binary Indexed Tree</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>