codeparrot/xlcost-text-to-code huggingface dataset:
Smallest power of 2 consisting of N digits | C ++ Program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNum ( int n ) { int res = 1 ; for ( int i = 2 ; ; i *= 2 ) { int length = log10 ( i ) + 1 ; if ( length == n ) return log ( i ) / log ( 2 ) ; } } int main ( ) { int n = 4 ; cout << smallestNum ( n ) ; return 0 ; }
Maximum weighted edge in path between two nodes in an N | C ++ implementation to find the maximum weighted edge in the simple path between two nodes in N - ary Tree ; Depths of Nodes ; Parent at every 2 ^ i level ; Maximum node at every 2 ^ i level ; Graph that stores destinations and its weight ; Function to traverse the nodes using the Depth - First Search Traversal ; Condition to check if its equal to its parent then skip ; DFS Recursive Call ; Function to find the ansector ; Loop to set every 2 ^ i distance ; Loop to calculate for each node in the N - ary tree ; Storing maximum edge ; Swaping if node a is at more depth than node b because we will always take at more depth ; Difference between the depth of the two given nodes ; Changing Node B to its parent at 2 ^ i distance ; Subtracting distance by 2 ^ i ; Take both a , b to its lca and find maximum ; Loop to find the 2 ^ ith parent that is different for both a and b i . e below the lca ; Updating ans ; Changing value to its parent ; Function to compute the Least common Ansector ; Driver Code ; Undirected tree ; Computing LCA
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 100005 ; vector < int > level ( N ) ; const int LG = 20 ; vector < vector < int > > dp ( LG , vector < int > ( N ) ) ; vector < vector < int > > mx ( LG , vector < int > ( N ) ) ; vector < vector < pair < int , int > > > v ( N ) ; int n ; void dfs_lca ( int a , int par , int lev ) { dp [ 0 ] [ a ] = par ; level [ a ] = lev ; for ( auto i : v [ a ] ) { if ( i . first == par ) continue ; mx [ 0 ] [ i . first ] = i . second ; dfs_lca ( i . first , a , lev + 1 ) ; } } void find_ancestor ( ) { for ( int i = 1 ; i < LG ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ; mx [ i ] [ j ] = max ( mx [ i - 1 ] [ j ] , mx [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ) ; } } } int getMax ( int a , int b ) { if ( level [ b ] < level [ a ] ) swap ( a , b ) ; int ans = 0 ; int diff = level [ b ] - level [ a ] ; while ( diff > 0 ) { int log = log2 ( diff ) ; ans = max ( ans , mx [ log ] [ b ] ) ; b = dp [ log ] [ b ] ; diff -= ( 1 << log ) ; } while ( a != b ) { int i = log2 ( level [ a ] ) ; while ( i > 0 && dp [ i ] [ a ] == dp [ i ] [ b ] ) i -- ; ans = max ( ans , mx [ i ] [ a ] ) ; ans = max ( ans , mx [ i ] [ b ] ) ; a = dp [ i ] [ a ] ; b = dp [ i ] [ b ] ; } return ans ; } void compute_lca ( ) { dfs_lca ( 1 , 0 , 0 ) ; find_ancestor ( ) ; } int main ( ) { n = 5 ; v [ 1 ] . push_back ( make_pair ( 2 , 2 ) ) ; v [ 2 ] . push_back ( make_pair ( 1 , 2 ) ) ; v [ 1 ] . push_back ( make_pair ( 3 , 5 ) ) ; v [ 3 ] . push_back ( make_pair ( 1 , 5 ) ) ; v [ 3 ] . push_back ( make_pair ( 4 , 3 ) ) ; v [ 4 ] . push_back ( make_pair ( 3 , 4 ) ) ; v [ 3 ] . push_back ( make_pair ( 5 , 1 ) ) ; v [ 5 ] . push_back ( make_pair ( 3 , 1 ) ) ; compute_lca ( ) ; int queries [ ] [ 2 ] = { { 3 , 5 } , { 2 , 3 } , { 2 , 4 } } ; int q = 3 ; for ( int i = 0 ; i < q ; i ++ ) { int max_edge = getMax ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ; cout << max_edge << endl ; } return 0 ; }
Count full nodes in a Binary tree ( Iterative and Recursive ) | C ++ program to count full nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of full Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; unsigned int getfullCount ( struct Node * root ) { if ( root == NULL ) return 0 ; int res = 0 ; if ( root -> left && root -> right ) res ++ ; res += ( getfullCount ( root -> left ) + getfullCount ( root -> right ) ) ; return res ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( void ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; cout << getfullCount ( root ) ; return 0 ; }
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | C ++ implementation to count decodings ; A Dynamic Programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change bool to int ; Return the required answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDecodingDP ( string digits , int n ) { if ( digits [ 0 ] == '0' ) return 0 ; int count0 = 1 , count1 = 1 , count2 ; for ( int i = 2 ; i <= n ; i ++ ) { count2 = ( ( int ) ( digits [ i - 1 ] != '0' ) * count1 ) + ( int ) ( ( digits [ i - 2 ] == '1' ) or ( digits [ i - 2 ] == '2' and digits [ i - 1 ] < '7' ) ) * count0 ; count0 = count1 ; count1 = count2 ; } return count1 ; } int main ( ) { string digits = "1234" ; int n = digits . size ( ) ; cout << countDecodingDP ( digits , n ) ; return 0 ; }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | C ++ Program to find the maximum distance from a 0 - cell to a 1 - cell ; Function to find the maximum distance ; Set up top row and left column ; Pass one : top left to bottom right ; Check if there was no " One " Cell ; Set up top row and left column ; Past two : bottom right to top left ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDistance ( vector < vector < int > > & grid ) { if ( ! grid . size ( ) ) return -1 ; int N = grid . size ( ) ; int INF = 1000000 ; vector < vector < int > > dp ( N , vector < int > ( N , 0 ) ) ; grid [ 0 ] [ 0 ] = grid [ 0 ] [ 0 ] == 1 ? 0 : INF ; for ( int i = 1 ; i < N ; i ++ ) grid [ 0 ] [ i ] = grid [ 0 ] [ i ] == 1 ? 0 : grid [ 0 ] [ i - 1 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) grid [ i ] [ 0 ] = grid [ i ] [ 0 ] == 1 ? 0 : grid [ i - 1 ] [ 0 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { grid [ i ] [ j ] = grid [ i ] [ j ] == 1 ? 0 : min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] ) + 1 ; } } if ( grid [ N - 1 ] [ N - 1 ] >= INF ) return -1 ; int maxi = grid [ N - 1 ] [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ N - 1 ] [ i ] = min ( grid [ N - 1 ] [ i ] , grid [ N - 1 ] [ i + 1 ] + 1 ) ; maxi = max ( grid [ N - 1 ] [ i ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ i ] [ N - 1 ] = min ( grid [ i ] [ N - 1 ] , grid [ i + 1 ] [ N - 1 ] + 1 ) ; maxi = max ( grid [ i ] [ N - 1 ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 2 ; j >= 0 ; j -- ) { grid [ i ] [ j ] = min ( grid [ i ] [ j ] , min ( grid [ i + 1 ] [ j ] + 1 , grid [ i ] [ j + 1 ] + 1 ) ) ; maxi = max ( grid [ i ] [ j ] , maxi ) ; } } return ! maxi ? -1 : maxi ; } int main ( ) { vector < vector < int > > arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; cout << maxDistance ( arr ) << endl ; return 0 ; }
Minimize total cost without repeating same task in two consecutive iterations | C ++ implementation of the above approach ; Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Driver code ; Number of iterations ; Number of tasks ; Cost matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCost ( vector < vector < int > > cost_mat , int N , int M ) { vector < vector < int > > dp ( N , vector < int > ( M , 0 ) ) ; for ( int i = 0 ; i < M ; i ++ ) dp [ 0 ] [ i ] = cost_mat [ 0 ] [ i ] ; for ( int row = 1 ; row < N ; row ++ ) { for ( int curr_col = 0 ; curr_col < M ; curr_col ++ ) { int val = 999999999 ; for ( int prev_col = 0 ; prev_col < M ; prev_col ++ ) { if ( curr_col != prev_col ) val = min ( val , dp [ row - 1 ] [ prev_col ] ) ; } dp [ row ] [ curr_col ] = val + cost_mat [ row ] [ curr_col ] ; } } int ans = INT_MAX ; for ( int i = 0 ; i < M ; i ++ ) ans = min ( ans , dp [ N - 1 ] [ i ] ) ; return ans ; } int main ( ) { int N = 4 ; int M = 4 ; vector < vector < int > > cost_mat ; cost_mat = { { 4 , 5 , 3 , 2 } , { 6 , 2 , 8 , 1 } , { 6 , 2 , 2 , 1 } , { 0 , 5 , 5 , 1 } } ; cout << findCost ( cost_mat , N , M ) ; return 0 ; }
Split the given string into Primes : Digit DP | C ++ implementation of the above approach ; Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Here to_string ( ) is used for converting int to string ; A function to find the minimum number of segments the given string can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the Given Prefix can be split into Primes then for the remaining string from i to j Check if Prime . If yes calculate the minimum split till j ; To check if the substring from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getPrimesFromSeive ( set < string > & primes ) { bool prime [ 1000001 ] ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) primes . insert ( to_string ( i ) ) ; } } int splitIntoPrimes ( string number ) { int numLen = number . length ( ) ; int splitDP [ numLen + 1 ] ; memset ( splitDP , -1 , sizeof ( splitDP ) ) ; set < string > primes ; getPrimesFromSeive ( primes ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && ( primes . find ( number . substr ( 0 , i ) ) != primes . end ( ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != -1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( primes . find ( number . substr ( i , j ) ) != primes . end ( ) ) { if ( splitDP [ i + j ] == -1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } int main ( ) { cout << splitIntoPrimes ( "13499315" ) << " STRNEWLINE " ; cout << splitIntoPrimes ( "43" ) << " STRNEWLINE " ; return 0 ; }
Connect Nodes at same Level ( Level Order Traversal ) | Connect nodes at same level using level order traversal . ; Node structure ; Sets nextRight of all nodes of a tree ; null marker to represent end of current level ; Do Level order of tree using NULL markers ; next element in queue represents next node at current Level ; push left and right children of current node ; if queue is not empty , push NULL to mark nodes at this level are visited ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right , * nextRight ; } ; void connect ( struct Node * root ) { queue < Node * > q ; q . push ( root ) ; q . push ( NULL ) ; while ( ! q . empty ( ) ) { Node * p = q . front ( ) ; q . pop ( ) ; if ( p != NULL ) { p -> nextRight = q . front ( ) ; if ( p -> left ) q . push ( p -> left ) ; if ( p -> right ) q . push ( p -> right ) ; } else if ( ! q . empty ( ) ) q . push ( NULL ) ; } } struct Node * newnode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = node -> nextRight = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newnode ( 10 ) ; root -> left = newnode ( 8 ) ; root -> right = newnode ( 2 ) ; root -> left -> left = newnode ( 3 ) ; root -> right -> right = newnode ( 90 ) ; connect ( root ) ; printf ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ STRNEWLINE " " the ▁ tree ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) ▁ STRNEWLINE " ) ; printf ( " nextRight ▁ of ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , root -> data , root -> nextRight ? root -> nextRight -> data : -1 ) ; printf ( " nextRight ▁ of ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , root -> left -> data , root -> left -> nextRight ? root -> left -> nextRight -> data : -1 ) ; printf ( " nextRight ▁ of ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , root -> right -> data , root -> right -> nextRight ? root -> right -> nextRight -> data : -1 ) ; printf ( " nextRight ▁ of ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , root -> left -> left -> data , root -> left -> left -> nextRight ? root -> left -> left -> nextRight -> data : -1 ) ; printf ( " nextRight ▁ of ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , root -> right -> right -> data , root -> right -> right -> nextRight ? root -> right -> right -> nextRight -> data : -1 ) ; return 0 ; }
Number of ways to convert a character X to a string Y | C ++ implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1000000007 ; long long modInv ( long long a , long long p = MOD - 2 ) { long long s = 1 ; while ( p != 1 ) { if ( p % 2 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p /= 2 ; } return ( a * s ) % MOD ; } long long findCnt ( char x , string y ) { long long ans = 0 ; long long fact [ y . size ( ) + 1 ] = { 1 } ; for ( long long i = 1 ; i <= y . size ( ) ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( long long i = 0 ; i < y . size ( ) ; i ++ ) { if ( y [ i ] == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . size ( ) - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . size ( ) - 1 ) ] ; ans %= MOD ; return ans ; } int main ( ) { char x = ' a ' ; string y = " xxayy " ; cout << findCnt ( x , y ) ; return 0 ; }
Find the Largest divisor Subset in the Array | C ++ implementation of the approach ; Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element ; Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubSeq ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count [ n ] = { 1 } ; int prev [ n ] = { -1 } ; memset ( count , 1 , sizeof ( count ) ) ; memset ( prev , -1 , sizeof ( prev ) ) ; int max = 0 ; int maxprev = -1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } int i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != -1 ) cout << arr [ i ] << " ▁ " ; i = prev [ i ] ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findSubSeq ( arr , n ) ; return 0 ; }
Minimum number of integers required such that each Segment contains at least one of them | C ++ program for the above approach ; function to sort the 2D vector on basis of second element . ; Function to compute minimum number of points which cover all segments ; Sort the list of tuples by their second element . ; To store the solution ; Iterate over all the segments ; Get the start point of next segment ; Loop over all those segments whose start point is less than the end point of current segment ; Print the possibles values of M ; Driver code ; Starting points of segments ; Ending points of segments ; Insert ranges in points [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sortcol ( const pair < int , int > p1 , const pair < int , int > p2 ) { return p1 . second < p2 . second ; } void minPoints ( pair < int , int > points [ ] , int n ) { sort ( points , points + n , sortcol ) ; vector < int > coordinates ; int i = 0 ; while ( i < n ) { int seg = points [ i ] . second ; coordinates . push_back ( seg ) ; int p = i + 1 ; if ( p >= n ) break ; int arrived = points [ p ] . first ; while ( seg >= arrived ) { p += 1 ; if ( p >= n ) break ; arrived = points [ p ] . first ; } i = p ; } for ( auto point : coordinates ) cout << point << " ▁ " ; } int main ( ) { int n = 4 ; int start [ ] = { 4 , 1 , 2 , 5 } ; int end [ ] = { 7 , 3 , 5 , 6 } ; pair < int , int > points [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { points [ i ] = { start [ i ] , end [ i ] } ; } minPoints ( points , n ) ; return 0 ; }
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | C ++ implementation of the approach ; Function to find the minimum required sum using dynamic programming ; dp [ i ] [ j ] = minimum val of accommodation till j 'th index of the string  using i+1 number of buckets.  Final ans will be in dp[n-1][K-1] ; Corner cases ; Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication ; If k = 0 then this arrangement is not possible ; If no arrangement is possible then our answer will remain INT_MAX so return - 1 ; Driver code ; K buckets
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( string str , int K ) { int n = str . length ( ) ; long long dp [ K ] [ n ] ; memset ( dp , 0 , sizeof dp ) ; if ( n < K ) return -1 ; else if ( n == K ) return 0 ; long long zeroes = 0 , ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) zeroes ++ ; else ones ++ ; dp [ 0 ] [ i ] = ones * zeroes ; } for ( int s = 1 ; s < K ; s ++ ) { for ( int i = 0 ; i < n ; i ++ ) { dp [ s ] [ i ] = INT_MAX ; ones = 0 ; zeroes = 0 ; for ( int k = i ; k >= 0 ; k -- ) { if ( str [ k ] == '0' ) zeroes ++ ; else ones ++ ; dp [ s ] [ i ] = min ( dp [ s ] [ i ] , + ( ( k - 1 >= 0 ) ? ones * zeroes + dp [ s - 1 ] [ k - 1 ] : INT_MAX ) ) ; } } } return ( dp [ K - 1 ] [ n - 1 ] == INT_MAX ) ? -1 : dp [ K - 1 ] [ n - 1 ] ; } int main ( ) { string S = "0101" ; int K = 2 ; cout << solve ( S , K ) << endl ; return 0 ; }
Queries for bitwise AND in the index range [ L , R ] of the given array | C ++ implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  100000 NEW_LINE #define bitscount  32 NEW_LINE using namespace std ; int prefix_count [ bitscount ] [ MAX ] ; void findPrefixCount ( int arr [ ] , int n ) { for ( int i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; } } } int rangeAnd ( int l , int r ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int x ; if ( l == 0 ) x = prefix_count [ i ] [ r ] ; else x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; if ( x == r - l + 1 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } int main ( ) { int arr [ ] = { 7 , 5 , 3 , 5 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; findPrefixCount ( arr , n ) ; int queries [ ] [ 2 ] = { { 1 , 3 } , { 4 , 5 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << rangeAnd ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }
Longest path in a directed Acyclic graph | Dynamic Programming | C ++ program to find the longest path in the DAG ; Function to traverse the DAG and apply Dynamic Programming to find the longest path ; Mark as visited ; Traverse for all its children ; If not visited ; Store the max of the paths ; Function to add an edge ; Function that returns the longest path ; Dp array ; Visited array to know if the node has been visited previously or not ; Call DFS for every unvisited vertex ; Traverse and find the maximum of all dp [ i ] ; Driver Code ; Example - 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int node , vector < int > adj [ ] , int dp [ ] , bool vis [ ] ) { vis [ node ] = true ; for ( int i = 0 ; i < adj [ node ] . size ( ) ; i ++ ) { if ( ! vis [ adj [ node ] [ i ] ] ) dfs ( adj [ node ] [ i ] , adj , dp , vis ) ; dp [ node ] = max ( dp [ node ] , 1 + dp [ adj [ node ] [ i ] ] ) ; } } void addEdge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; } int findLongestPath ( vector < int > adj [ ] , int n ) { int dp [ n + 1 ] ; memset ( dp , 0 , sizeof dp ) ; bool vis [ n + 1 ] ; memset ( vis , false , sizeof vis ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! vis [ i ] ) dfs ( i , adj , dp , vis ) ; } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = max ( ans , dp [ i ] ) ; } return ans ; } int main ( ) { int n = 5 ; vector < int > adj [ n + 1 ] ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 2 , 4 ) ; addEdge ( adj , 3 , 4 ) ; cout << findLongestPath ( adj , n ) ; return 0 ; }