

- 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
First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++
<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 − array[] = {5, 2, 9, 4, 1}</p><p>prefixSumArray[] = {5, 7, 16, 20, 21}</p><p><strong>Let'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 'P' 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 'P'.</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 'P' −</p><p>Either the target position lies between '<em>position + 2^i</em>' and '<em>position + 2^(i+1)</em>' where the ith lift increased the value. Or, the target position lies between '<em>position</em>' and '<em>position + 2^i</em>'.</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 <iostream> #include <math.h> using namespace std; void generatePrefixSum(int arr[], int prefSum[], int n){ prefSum[0] = arr[0]; for (int i = 1; i < n; i++) prefSum[i] = prefSum[i - 1] + arr[i]; } int findPreSumIndexBL(int prefSum[], int n, int x){ int P = 0; int LOGN = log2(n); if (x <= prefSum[0]) return 0; for (int i = LOGN; i >= 0; i--) { if (P + (1 << i) < n && prefSum[P + (1 << i)] < x) { P += (1 << 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<<"The index of first elements of the array greater than the given number is "; cout<<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>
- Related Questions & Answers
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Sum of first N natural numbers which are divisible by X or Y
- Program to find X for special array with X elements greater than or equal X in Python
- Getting equal or greater than number from the list of numbers in JavaScript
- How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Minimum numbers which is smaller than or equal to N and with sum S in C++
- Find all factorial numbers less than or equal to n in C++
- Print all prime numbers less than or equal to N in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- All possible binary numbers of length n with equal sum in both halves?
- Print all Semi-Prime Numbers less than or equal to N in C++
- Sum of sum of first n natural numbers in C++
Advertisements