First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++

C++Server Side ProgrammingProgramming

<p>In this problem, we are given an array arr[] consisting of N numbers and an integer value x. Our task is to <em>create a program for finding the first element greater than or equal to X in the prefix sum of N numbers using Binary Lifting</em>.</p><p><strong>Prefix Sum</strong> of elements of an array is an array whose each element is the sum of all elements till that index in the initial array.</p><p>Example &minus; array[] = {5, 2, 9, 4, 1}</p><p>prefixSumArray[] = {5, 7, 16, 20, 21}</p><p><strong>Let&#39;s take an example to understand the problem,</strong></p><pre class="result notranslation">Input: arr[] = {5, 2, 9, 4, 1}, X = 19 Output: 3</pre><h2>Solution Approach</h2><p>Here, we will be solving the problem using the concept of <em>binary lifting</em>. Binary Lifting is increasing the value of the given number by powers of 2 (done by flipping bits) ranging from 0 to N.</p><p>We will be considering a concept similar to lifting binary trees where we will be finding the initial value of &#39;P&#39; index. This is increased by flipping bits making sure the value is not greater than X. Now, we will be considering lift along with this position &#39;P&#39;.</p><p>For this, we will start flipping bits of the number such that i-th bit flip does not make the sum greater than X. Now, we have two cases based on the value of &#39;P&#39; &minus;</p><p>Either the target position lies between &#39;<em>position + 2^i</em>&#39; and &#39;<em>position + 2^(i+1)</em>&#39; where the ith lift increased the value. Or, the target position lies between &#39;<em>position</em>&#39; and &#39;<em>position + 2^i</em>&#39;.</p><p>Using this we will be considering the index position.</p><h2>Example</h2><p>Program to illustrate the working of our solution</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std; void generatePrefixSum(int arr[], int prefSum[], int n){ &nbsp; &nbsp;prefSum[0] = arr[0]; &nbsp; &nbsp;for (int i = 1; i &lt; n; i++) &nbsp; &nbsp; &nbsp; prefSum[i] = prefSum[i - 1] + arr[i]; } int findPreSumIndexBL(int prefSum[], int n, int x){ &nbsp; &nbsp;int P = 0; &nbsp; &nbsp;int LOGN = log2(n); &nbsp; &nbsp;if (x &lt;= prefSum[0]) &nbsp; &nbsp; &nbsp; return 0; for (int i = LOGN; i &gt;= 0; i--) { if (P + (1 &lt;&lt; i) &lt; n &amp;&amp; prefSum[P + (1 &lt;&lt; i)] &lt; x) { P += (1 &lt;&lt; i); } } return P + 1; } int main(){ int arr[] = { 5, 2, 9, 4, 1 }; int X = 19; int n = sizeof(arr) / sizeof(arr[0]); int prefSum[n] = { 0 }; generatePrefixSum(arr, prefSum, n); cout&lt;&lt;&quot;The index of first elements of the array greater than the given number is &quot;; cout&lt;&lt;findPreSumIndexBL(prefSum, n, X); return 0; }</pre><h2>Output</h2><pre class="result notranslate">The index of first elements of the array greater than the given number is 3 </pre>
raja
Updated on 01-Feb-2022 10:56:48

Advertisements