codeparrot/xlcost-text-to-code huggingface dataset:
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | C ++ program to do range minimum query in O ( 1 ) time with O ( n * n ) extra space and O ( n * n ) preprocessing time . ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] ; Structure to represent a query range ; Fills lookup array lookup [ n ] [ n ] for all possible values of query ranges ; Initialize lookup [ ] [ ] for the intervals with length 1 ; Fill rest of the entries in bottom up manner ; To find minimum of [ 0 , 4 ] , we compare minimum of arr [ lookup [ 0 ] [ 3 ] ] with arr [ 4 ] . ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fill lookup table for all possible input queries ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  500 NEW_LINE int lookup [ MAX ] [ MAX ] ; struct Query { int L , R ; } ; void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ i ] = i ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ j ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = j ; } } void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; cout << " Minimum ▁ of ▁ [ " << L << " , ▁ " << R << " ] ▁ is ▁ " << arr [ lookup [ L ] [ R ] ] << endl ; } } int main ( ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Query q [ ] = { { 0 , 4 } , { 4 , 7 } , { 7 , 8 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; RMQ ( a , n , q , m ) ; return 0 ; }
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | C ++ program to do range minimum query in O ( 1 ) time with O ( n Log n ) extra space and O ( n Log n ) preprocessing time ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] . Ideally lookup table size should not be fixed and should be determined using n Log n . It is kept constant to keep code simple . ; Structure to represent a query range ; Fills lookup array lookup [ ] [ ] in bottom up manner . ; Initialize M for the intervals with length 1 ; Compute values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 ] [ 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] ; Returns minimum of arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] , ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fills table lookup [ n ] [ Log n ] ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  500 NEW_LINE int lookup [ MAX ] [ MAX ] ; struct Query { int L , R ; } ; void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } int query ( int arr [ ] , int L , int R ) { int j = ( int ) log2 ( R - L + 1 ) ; if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) return arr [ lookup [ L ] [ j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ; } void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; cout << " Minimum ▁ of ▁ [ " << L << " , ▁ " << R << " ] ▁ is ▁ " << query ( arr , L , R ) << endl ; } } int main ( ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Query q [ ] = { { 0 , 4 } , { 4 , 7 } , { 7 , 8 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; RMQ ( a , n , q , m ) ; return 0 ; }
Find sum of all nodes of the given perfect binary tree |  ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; int sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; int sum = sumLastLevel * l ; return sum ; } int main ( ) { int l = 3 ; cout << sumNodes ( l ) ; return 0 ; }
Longest subsequence with different adjacent characters | C ++ program for the above approach ; dp table ; A recursive function to find the update the dp [ ] [ ] table ; If we reach end of the string ; If subproblem has been computed ; Initialise variable to find the maximum length ; Choose the current character ; Omit the current character ; Return the store answer to the current subproblem ; Function to find the longest Subsequence with different adjacent character ; Length of the string s ; Initialise the memoisation table ; Return the final ans after every recursive call ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100005 ] [ 27 ] ; int calculate ( int pos , int prev , string & s ) { if ( pos == s . length ( ) ) { return 0 ; } if ( dp [ pos ] [ prev ] != -1 ) return dp [ pos ] [ prev ] ; int val = 0 ; if ( s [ pos ] - ' a ' + 1 != prev ) { val = max ( val , 1 + calculate ( pos + 1 , s [ pos ] - ' a ' + 1 , s ) ) ; } val = max ( val , calculate ( pos + 1 , prev , s ) ) ; return dp [ pos ] [ prev ] = val ; } int longestSubsequence ( string s ) { int n = s . length ( ) ; memset ( dp , -1 , sizeof ( dp ) ) ; return calculate ( 0 , 0 , s ) ; } int main ( ) { string str = " ababa " ; cout << longestSubsequence ( str ) ; return 0 ; }
Constant time range add operation on an array | C ++ program to get updated array after many array range add operation ; Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void add ( int arr [ ] , int N , int lo , int hi , int val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } void updateArray ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } void printArr ( int arr [ ] , int N ) { updateArray ( arr , N ) ; for ( int i = 0 ; i < N ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int N = 6 ; int arr [ N ] = { 0 } ; add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ; return 0 ; }
Count of elements in an Array whose set bits are in a multiple of K | C ++ implementation of above approach ; Function to find the count of numbers ; Get the set - bits count of each element ; Check if the setbits count is divisible by K ; Increment the count of required numbers by 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_count ( vector < int > arr , int k ) { int ans = 0 ; for ( int i : arr ) { int x = __builtin_popcount ( i ) ; if ( x % k == 0 ) ans += 1 ; } return ans ; } int main ( ) { vector < int > arr = { 12 , 345 , 2 , 68 , 7896 } ; int K = 2 ; cout << find_count ( arr , K ) ; return 0 ; }
Check if a number can be expressed as a product of exactly K prime divisors | CPP implementation to Check if a number can be expressed as a product of exactly K prime divisors ; function to find K prime divisors ; count number of 2 s that divide N ; N must be odd at this point . So we can skip one element ; divide the value of N ; increment count ; Condition to handle the case when n is a prime number greater than 2 ; check if maximum_split is less than K then it not possible ; Driver code ; initialise N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void KPrimeDivisors ( int N , int K ) { int maximum_split = 0 ; while ( N % 2 == 0 ) { maximum_split ++ ; N /= 2 ; } for ( int i = 3 ; i * i <= N ; i = i + 2 ) { while ( N % i == 0 ) { N = N / i ; maximum_split ++ ; } } if ( N > 2 ) maximum_split ++ ; if ( maximum_split < K ) { printf ( " No STRNEWLINE " ) ; return ; } printf ( " Yes STRNEWLINE " ) ; } int main ( ) { int N = 12 ; int K = 3 ; KPrimeDivisors ( N , K ) ; return 0 ; }
Maximum profit by buying and selling a share at most K times | Greedy Approach | C ++ program to find out maximum profit by buying and selling a share at most k times given the stock price of n days ; Function to return the maximum profit ; Find the farthest decreasing span of prices before prices rise ; Find the farthest increasing span of prices before prices fall again ; Check if the current buying price is greater than that of the previous transaction ; Store the profit ; Remove the previous transaction ; Check if the current selling price is less than that of the previous transactions ; Store the new profit ; Remove the previous transaction ; Add the new transactions ; Finally store the profits of all the transactions ; Add the highest K profits ; Return the maximum profit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int n , int k , int prices [ ] ) { int ans = 0 , buy = 0 , sell = 0 ; stack < pair < int , int > > transaction ; priority_queue < int > profits ; while ( sell < n ) { buy = sell ; while ( buy < n - 1 && prices [ buy ] >= prices [ buy + 1 ] ) buy ++ ; sell = buy + 1 ; while ( sell < n && prices [ sell ] >= prices [ sell - 1 ] ) sell ++ ; while ( ! transaction . empty ( ) && prices [ buy ] < prices [ transaction . top ( ) . first ] ) { pair < int , int > p = transaction . top ( ) ; profits . push ( prices [ p . second - 1 ] - prices [ p . first ] ) ; transaction . pop ( ) ; } while ( ! transaction . empty ( ) && prices [ sell - 1 ] > prices [ transaction . top ( ) . second - 1 ] ) { pair < int , int > p = transaction . top ( ) ; profits . push ( prices [ p . second - 1 ] - prices [ buy ] ) ; buy = p . first ; transaction . pop ( ) ; } transaction . push ( { buy , sell } ) ; } while ( ! transaction . empty ( ) ) { profits . push ( prices [ transaction . top ( ) . second - 1 ] - prices [ transaction . top ( ) . first ] ) ; transaction . pop ( ) ; } while ( k && ! profits . empty ( ) ) { ans += profits . top ( ) ; profits . pop ( ) ; -- k ; } return ans ; } int main ( ) { int k = 3 ; int prices [ ] = { 1 , 12 , 10 , 7 , 10 , 13 , 11 , 10 , 7 , 6 , 9 } ; int n = sizeof ( prices ) / sizeof ( prices [ 0 ] ) ; cout << " Maximum ▁ profit ▁ is ▁ " << maxProfit ( n , k , prices ) ; return 0 ; }
GCDs of given index ranges in an array | C ++ Program to find GCD of a number in a given Range using segment Trees ; To store segment tree ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; A recursive function to get gcd of given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree si -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ index ] qs & qe -- > Starting and ending indexes of query range ; Finding The gcd of given Range ; Driver program to test above functions ; Starting index of range . These indexes are 0 based . ; Last index of range . These indexes are 0 based .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * st ; int constructST ( int arr [ ] , int ss , int se , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return st [ si ] ; } int mid = ss + ( se - ss ) / 2 ; st [ si ] = __gcd ( constructST ( arr , ss , mid , si * 2 + 1 ) , constructST ( arr , mid + 1 , se , si * 2 + 2 ) ) ; return st [ si ] ; } int * constructSegmentTree ( int arr [ ] , int n ) { int height = ( int ) ( ceil ( log2 ( n ) ) ) ; int size = 2 * ( int ) pow ( 2 , height ) - 1 ; st = new int [ size ] ; constructST ( arr , 0 , n - 1 , 0 ) ; return st ; } int findGcd ( int ss , int se , int qs , int qe , int si ) { if ( ss > qe se < qs ) return 0 ; if ( qs <= ss && qe >= se ) return st [ si ] ; int mid = ss + ( se - ss ) / 2 ; return __gcd ( findGcd ( ss , mid , qs , qe , si * 2 + 1 ) , findGcd ( mid + 1 , se , qs , qe , si * 2 + 2 ) ) ; } int findRangeGcd ( int ss , int se , int arr [ ] , int n ) { if ( ss < 0 se > n -1 ss > se ) { cout << " Invalid ▁ Arguments " << " STRNEWLINE " ; return - 1 ; } return findGcd ( 0 , n - 1 , ss , se , 0 ) ; } int main ( ) { int a [ ] = { 2 , 3 , 6 , 9 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; constructSegmentTree ( a , n ) ; int l = 1 ; int r = 3 ; cout << " GCD ▁ of ▁ the ▁ given ▁ range ▁ is : " ; cout << findRangeGcd ( l , r , a , n ) << " STRNEWLINE " ; return 0 ; }
Find sum in range L to R in given sequence of integers | C ++ program to find the sum in given range L to R ; Function to find the sum within the given range ; generating array from given sequence ; calculate the desired sum ; return the sum ; Driven code ; initialise the range
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int L , int R ) { vector < int > arr ; int i = 0 ; int x = 2 ; while ( i <= R ) { arr . push_back ( i + x ) ; if ( i + 1 <= R ) arr . push_back ( i + 1 + x ) ; x *= -1 ; i += 2 ; } int sum = 0 ; for ( int i = L ; i <= R ; ++ i ) sum += arr [ i ] ; return sum ; } int main ( ) { int L = 0 , R = 5 ; cout << findSum ( L , R ) ; return 0 ; }
Queries for GCD of all numbers of an array except elements in a given range | C ++ program for queries of GCD excluding given range of elements . ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = __gcd ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = __gcd ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FillPrefixSuffix ( int prefix [ ] , int arr [ ] , int suffix [ ] , int n ) { prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = __gcd ( prefix [ i - 1 ] , arr [ i ] ) ; suffix [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix [ i ] = __gcd ( suffix [ i + 1 ] , arr [ i ] ) ; } int GCDoutsideRange ( int l , int r , int prefix [ ] , int suffix [ ] , int n ) { if ( l == 0 ) return suffix [ r + 1 ] ; if ( r == n - 1 ) return prefix [ l - 1 ] ; return __gcd ( prefix [ l - 1 ] , suffix [ r + 1 ] ) ; } int main ( ) { int arr [ ] = { 2 , 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int prefix [ n ] , suffix [ n ] ; FillPrefixSuffix ( prefix , arr , suffix , n ) ; int l = 0 , r = 0 ; cout << GCDoutsideRange ( l , r , prefix , suffix , n ) << endl ; l = 1 ; r = 1 ; cout << GCDoutsideRange ( l , r , prefix , suffix , n ) << endl ; l = 1 ; r = 2 ; cout << GCDoutsideRange ( l , r , prefix , suffix , n ) << endl ; return 0 ; }
Count of pairs from arrays A and B such that element in A is greater than element in B at that index | C ++ program to find the maximum count of values that follow the given condition ; Function to find the maximum count of values that follow the given condition ; Initializing the max - heap for the array A [ ] ; Adding the values of A [ ] into max heap ; Adding the values of B [ ] into max heap ; Counter variable ; Loop to iterate through the heap ; Comparing the values at the top . If the value of heap A [ ] is greater , then counter is incremented ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int A [ ] , int B [ ] , int N ) { priority_queue < int > pq1 , pq2 ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . push ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . push ( B [ i ] ) ; } int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 . top ( ) > pq2 . top ( ) ) { c ++ ; pq1 . pop ( ) ; pq2 . pop ( ) ; } else { if ( pq2 . size ( ) == 0 ) { break ; } pq2 . pop ( ) ; } } return ( c ) ; } int main ( ) { int A [ ] = { 10 , 3 , 7 , 5 , 8 } ; int B [ ] = { 8 , 6 , 2 , 5 , 9 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << ( check ( A , B , N ) ) ; }
Number of elements less than or equal to a given number in a given subarray | Set 2 ( Including Updates ) | Number of elements less than or equal to a given number in a given subarray and allowing update operations . ; updating the bit array of a valid block ; answering the query ; traversing the first block in range ; Traversing completely overlapped blocks in range for such blocks bit array of that block is queried ; Traversing the last block ; Preprocessing the array ; updating the bit array at the original and new value of array ; driver function ; size of block size will be equal to square root of n ; initialising bit array of each block as elements of array cannot exceed 10 ^ 4 so size of bit array is accordingly
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10001 ; void update ( int idx , int blk , int val , int bit [ ] [ MAX ] ) { for ( ; idx < MAX ; idx += ( idx & - idx ) ) bit [ blk ] [ idx ] += val ; } int query ( int l , int r , int k , int arr [ ] , int blk_sz , int bit [ ] [ MAX ] ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } while ( l + blk_sz <= r ) { int idx = k ; for ( ; idx > 0 ; idx -= idx & - idx ) sum += bit [ l / blk_sz ] [ idx ] ; l += blk_sz ; } while ( l <= r ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } return sum ; } void preprocess ( int arr [ ] , int blk_sz , int n , int bit [ ] [ MAX ] ) { for ( int i = 0 ; i < n ; i ++ ) update ( arr [ i ] , i / blk_sz , 1 , bit ) ; } void preprocessUpdate ( int i , int v , int blk_sz , int arr [ ] , int bit [ ] [ MAX ] ) { update ( arr [ i ] , i / blk_sz , -1 , bit ) ; update ( v , i / blk_sz , 1 , bit ) ; arr [ i ] = v ; } int main ( ) { int arr [ ] = { 5 , 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int blk_sz = sqrt ( n ) ; int bit [ blk_sz + 1 ] [ MAX ] ; memset ( bit , 0 , sizeof ( bit ) ) ; preprocess ( arr , blk_sz , n , bit ) ; cout << query ( 1 , 3 , 1 , arr , blk_sz , bit ) << endl ; preprocessUpdate ( 3 , 10 , blk_sz , arr , bit ) ; cout << query ( 3 , 3 , 4 , arr , blk_sz , bit ) << endl ; preprocessUpdate ( 2 , 1 , blk_sz , arr , bit ) ; preprocessUpdate ( 0 , 2 , blk_sz , arr , bit ) ; cout << query ( 0 , 4 , 5 , arr , blk_sz , bit ) << endl ; return 0 ; }
Queries for counts of array elements with values in given range | Simple C ++ program to count number of elements with values in given range . ; function to count elements within given range ; initialize result ; check if element is in range ; driver function ; Answer queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countInRange ( int arr [ ] , int n , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 3 , 4 , 9 , 10 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int i = 1 , j = 4 ; cout << countInRange ( arr , n , i , j ) << endl ; i = 9 , j = 12 ; cout << countInRange ( arr , n , i , j ) << endl ; return 0 ; }
Path with smallest product of edges with weight >= 1 | C ++ implementation of the approach ; Function to return the smallest product of edges ; If the source is equal to the destination ; Initialise the priority queue ; Visited array ; While the priority - queue is not empty ; Current node ; Current product of distance ; Popping the top - most element ; If already visited continue ; Marking the node as visited ; If it is a destination node ; Traversing the current node ; If no path exists ; Driver code ; Graph as adjacency matrix ; Input edges ; Source and destination ; Dijkstra
#include <bits/stdc++.h> NEW_LINE using namespace std ; double dijkstra ( int s , int d , vector < vector < pair < int , double > > > gr ) { if ( s == d ) return 0 ; set < pair < int , int > > pq ; pq . insert ( { 1 , s } ) ; bool v [ gr . size ( ) ] = { 0 } ; while ( pq . size ( ) ) { int curr = pq . begin ( ) -> second ; int dist = pq . begin ( ) -> first ; pq . erase ( pq . begin ( ) ) ; if ( v [ curr ] ) continue ; v [ curr ] = 1 ; if ( curr == d ) return dist ; for ( auto it : gr [ curr ] ) pq . insert ( { dist * it . second , it . first } ) ; } return -1 ; } int main ( ) { int n = 3 ; vector < vector < pair < int , double > > > gr ( n + 1 ) ; gr [ 1 ] . push_back ( { 3 , 9 } ) ; gr [ 2 ] . push_back ( { 3 , 1 } ) ; gr [ 1 ] . push_back ( { 2 , 5 } ) ; int s = 1 , d = 3 ; cout << dijkstra ( s , d , gr ) ; return 0 ; }
Queries for counts of array elements with values in given range | Efficient C ++ program to count number of elements with values in given range . ; function to find first index >= x ; function to find last index <= y ; function to count elements within given range ; initialize result ; driver function ; Preprocess array ; Answer queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lowerIndex ( int arr [ ] , int n , int x ) { int l = 0 , h = n - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] >= x ) h = mid - 1 ; else l = mid + 1 ; } return l ; } int upperIndex ( int arr [ ] , int n , int y ) { int l = 0 , h = n - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] <= y ) l = mid + 1 ; else h = mid - 1 ; } return h ; } int countInRange ( int arr [ ] , int n , int x , int y ) { int count = 0 ; count = upperIndex ( arr , n , y ) - lowerIndex ( arr , n , x ) + 1 ; return count ; } int main ( ) { int arr [ ] = { 1 , 4 , 4 , 9 , 10 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , arr + n ) ; int i = 1 , j = 4 ; cout << countInRange ( arr , n , i , j ) << endl ; i = 9 , j = 12 ; cout << countInRange ( arr , n , i , j ) << endl ; return 0 ; }
Queries for decimal values of subarrays of a binary array | C ++ implementation of finding number represented by binary subarray ; Fills pre [ ] ; returns the number represented by a binary subarray l to r ; if r is equal to n - 1 r + 1 does not exist ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void precompute ( int arr [ ] , int n , int pre [ ] ) { memset ( pre , 0 , n * sizeof ( int ) ) ; pre [ n - 1 ] = arr [ n - 1 ] * pow ( 2 , 0 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) pre [ i ] = pre [ i + 1 ] + arr [ i ] * ( 1 << ( n - 1 - i ) ) ; } int decimalOfSubarr ( int arr [ ] , int l , int r , int n , int pre [ ] ) { if ( r != n - 1 ) return ( pre [ l ] - pre [ r + 1 ] ) / ( 1 << ( n - 1 - r ) ) ; return pre [ l ] / ( 1 << ( n - 1 - r ) ) ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int pre [ n ] ; precompute ( arr , n , pre ) ; cout << decimalOfSubarr ( arr , 2 , 4 , n , pre ) << endl ; cout << decimalOfSubarr ( arr , 4 , 5 , n , pre ) << endl ; return 0 ; }