codeparrot/xlcost-text-to-code huggingface dataset:
Iterative approach to print all combinations of an Array | C ++ implementation of the approach ; Data array for combination ; Number of elements in the combination ; The boolean array ; Starting index of the 1 st tract of trues ; Ending index of the 1 st tract of trues ; Constructor ; Set the 1 st r Booleans to true , initialize Start and End ; Generate the very first combination ; Update the starting ending indices of trues in the boolean array ; Function that returns true if another combination can still be generated ; Function to generate the next combination ; Only one true in the tract ; Move the End and reset the End ; Set all the values to false starting from index Start and ending at index End in the boolean array ; Set the beginning elements to true ; Reset the End ; Function to print the combination generated previouslt ; If current index is set to true in the boolean array then element at current index in the original array is part of the combination generated previously ; Driver code
#include <iostream> NEW_LINE using namespace std ; class Combination { private : int * Indices ; int N ; int R ; bool * Flags ; int Start ; int End ; public : Combination ( int * arr , int n , int r ) { this -> Indices = arr ; this -> N = n ; this -> R = r ; this -> Flags = nullptr ; } ~ Combination ( ) { if ( this -> Flags != nullptr ) { delete [ ] this -> Flags ; } } void GetFirst ( ) { this -> Flags = new bool [ N ] ; for ( int i = 0 ; i < this -> N ; ++ i ) { if ( i < this -> R ) { Flags [ i ] = true ; } else { Flags [ i ] = false ; } } this -> Start = 0 ; this -> End = this -> R - 1 ; this -> Output ( ) ; } bool HasNext ( ) { return End < ( this -> N - 1 ) ; } void Next ( ) { if ( this -> Start == this -> End ) { this -> Flags [ this -> End ] = false ; this -> Flags [ this -> End + 1 ] = true ; this -> Start += 1 ; this -> End += 1 ; while ( this -> End + 1 < this -> N && this -> Flags [ this -> End + 1 ] ) { ++ this -> End ; } } else { if ( this -> Start == 0 ) { Flags [ this -> End ] = false ; Flags [ this -> End + 1 ] = true ; this -> End -= 1 ; } else { Flags [ this -> End + 1 ] = true ; for ( int i = this -> Start ; i <= this -> End ; ++ i ) { Flags [ i ] = false ; } for ( int i = 0 ; i < this -> End - this -> Start ; ++ i ) { Flags [ i ] = true ; } this -> End = this -> End - this -> Start - 1 ; this -> Start = 0 ; } } this -> Output ( ) ; } private : void Output ( ) { for ( int i = 0 , count = 0 ; i < this -> N && count < this -> R ; ++ i ) { if ( Flags [ i ] ) { cout << Indices [ i ] << " ▁ " ; ++ count ; } } cout << endl ; } } ; int main ( ) { int arr [ ] = { 0 , 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int r = 3 ; Combination com ( arr , n , r ) ; com . GetFirst ( ) ; while ( com . HasNext ( ) ) { com . Next ( ) ; } return 0 ; }
Number of ways to choose elements from the array such that their average is K | C ++ implementation of the above approach ; This dp array is used to store our values so that we don 't have to calculate same values again and again ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Initialize dp array by - 1 ; Call recursive function waysutil to calculate total ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_INDEX  51 NEW_LINE #define MAX_SUM  2505 NEW_LINE int dp [ MAX_INDEX ] [ MAX_SUM ] [ MAX_INDEX ] ; int waysutil ( int index , int sum , int count , vector < int > & arr , int K ) { if ( index < 0 ) return 0 ; if ( index == 0 ) { if ( count == 0 ) return 0 ; int remainder = sum % count ; if ( remainder != 0 ) return 0 ; int average = sum / count ; if ( average == K ) return 1 ; } if ( dp [ index ] [ sum ] [ count ] != -1 ) return dp [ index ] [ sum ] [ count ] ; int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index ] [ sum ] [ count ] = total ; return total ; } int ways ( int N , int K , int * arr ) { vector < int > Arr ; Arr . push_back ( -1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . push_back ( arr [ i ] ) ; } memset ( dp , -1 , sizeof dp ) ; int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } int main ( ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; cout << ways ( N , K , arr ) ; return 0 ; }
Subset with sum closest to zero | Variable to store states of dp ; Variable to store states of dp ; Function to return the number closer to integer s ; To find the sum closest to zero Since sum can be negative , we will add MAX to it to make it positive ; Base cases ; Checks if a state is already solved ; Recurrence relation ; Returning the value ; Function to calculate the closest sum value ; Calculate the Closest value for every subarray arr [ i - 1 : n ] ; Driver function ; Input array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define arrSize  51 NEW_LINE #define maxSum  201 NEW_LINE #define MAX  100 NEW_LINE #define inf  999999 NEW_LINE int dp [ arrSize ] [ maxSum ] ; bool visit [ arrSize ] [ maxSum ] ; int RetClose ( int a , int b , int s ) { if ( abs ( a - s ) < abs ( b - s ) ) return a ; else return b ; } int MinDiff ( int i , int sum , int arr [ ] , int n ) { if ( i == n ) return 0 ; if ( visit [ i ] [ sum + MAX ] ) return dp [ i ] [ sum + MAX ] ; visit [ i ] [ sum + MAX ] = 1 ; dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , -1 * sum ) ; return dp [ i ] [ sum + MAX ] ; } void FindClose ( int arr [ ] , int n ) { int ans = inf ; for ( int i = 1 ; i <= n ; i ++ ) ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; cout << ans << endl ; } int main ( ) { int arr [ ] = { 25 , -9 , -10 , -4 , -7 , -33 } ; int n = sizeof ( arr ) / sizeof ( int ) ; FindClose ( arr , n ) ; return 0 ; }
Paths from entry to exit in matrix and maximum path sum | C ++ implementation of the approach ; Recursive function to return the total paths from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Recursive function to return the maximum sum along the path from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Driver code ; Fill the dp [ ] [ ] array with - 1 ; When source and destination are same then there is only 1 path ; Print the count of paths from grid [ 0 ] [ 0 ] to grid [ n - 1 ] [ n - 1 ] ; Fill the dp [ ] [ ] array again with - 1 for ( int i = 0 ; i < n ; i ++ ) Arrays . fill ( dp [ i ] , - 1 ) ; ; When source and destination are same then the sum is grid [ n - 1 ] [ n - 1 ] ; Print the maximum sum among all the paths from grid [ 0 ] [ 0 ] to grid [ n - 1 ] [ n - 1 ]
#include <bits/stdc++.h> NEW_LINE #define COL  5 NEW_LINE #define ROW  5 NEW_LINE using namespace std ; int totalPaths ( int i , int j , int n , int grid [ ] [ COL ] , int dp [ ] [ COL ] ) { if ( i < 0 j < 0 i > = n j > = n ) return 0 ; if ( dp [ i ] [ j ] == -1 ) { if ( grid [ i ] [ j ] == 1 ) dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i ] [ j ] == 2 ) dp [ i ] [ j ] = totalPaths ( i + 1 , j , n , grid , dp ) ; else dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp ) ; } return dp [ i ] [ j ] ; } int maxSumPath ( int i , int j , int n , int grid [ ROW ] [ COL ] , int dp [ ROW ] [ COL ] ) { if ( i < 0 j < 0 i > = n j > = n ) return 0 ; if ( dp [ i ] [ j ] == -1 ) { if ( grid [ i ] [ j ] == 1 ) dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i ] [ j ] == 2 ) dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i + 1 , j , n , grid , dp ) ; else dp [ i ] [ j ] = grid [ i ] [ j ] + max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) ) ; } return dp [ i ] [ j ] ; } int main ( ) { int grid [ ROW ] [ COL ] = { { 1 , 1 , 3 , 2 , 1 } , { 3 , 2 , 2 , 1 , 2 } , { 1 , 3 , 3 , 1 , 3 } , { 1 , 2 , 3 , 1 , 2 } , { 1 , 1 , 1 , 3 , 1 } } ; int n = ROW ; int dp [ ROW ] [ COL ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = -1 ; } dp [ n - 1 ] [ n - 1 ] = 1 ; cout << " Total ▁ paths : ▁ " << totalPaths ( 0 , 0 , n , grid , dp ) << endl ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = -1 ; } dp [ n - 1 ] [ n - 1 ] = grid [ n - 1 ] [ n - 1 ] ; cout << " Maximum ▁ sum : ▁ " << maxSumPath ( 0 , 0 , n , grid , dp ) << endl ; return 0 ; }
Queries for bitwise OR 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 rangeOr ( 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 != 0 ) 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 << rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }
Distinct palindromic sub | C ++ implementation of the approach ; Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i ] [ i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 ] [ end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start ] [ end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int palindromeSubStrs ( string s ) { int dp [ s . size ( ) ] [ s . size ( ) ] ; int st , end , i , j , len ; map < string , bool > m ; for ( i = 0 ; i < s . size ( ) ; i ++ ) { dp [ i ] [ i ] = 1 ; m [ string ( s . begin ( ) + i , s . begin ( ) + i + 1 ) ] = 1 ; } for ( i = 0 ; i < s . size ( ) - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { dp [ i ] [ i + 1 ] = 1 ; m [ string ( s . begin ( ) + i , s . begin ( ) + i + 2 ) ] = 1 ; } else { dp [ i ] [ i + 1 ] = 0 ; } } for ( len = 3 ; len <= s . size ( ) ; len ++ ) { for ( st = 0 ; st <= s . size ( ) - len ; st ++ ) { end = st + len - 1 ; if ( s [ st ] == s [ end ] && dp [ st + 1 ] [ end - 1 ] ) { dp [ st ] [ end ] = 1 ; m [ string ( s . begin ( ) + st , s . begin ( ) + end + 1 ) ] = 1 ; } else dp [ st ] [ end ] = 0 ; } } return m . size ( ) ; } int main ( ) { string s = " abaaa " ; cout << palindromeSubStrs ( s ) ; return 0 ; }
Maximum sum path in a matrix from top to bottom and back | C ++ implementation of the approach ; Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; Driver code ; set initial value
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n = 4 , m = 4 ; int arr [ 4 ] [ 4 ] = { { 1 , 0 , 3 , -1 } , { 3 , 5 , 1 , -2 } , { -2 , 0 , 1 , 1 } , { 2 , 1 , -1 , 1 } } ; int cache [ 5 ] [ 5 ] [ 5 ] ; int sum ( int i1 , int j1 , int i2 , int j2 ) { if ( i1 == i2 && j1 == j2 ) { return arr [ i1 ] [ j1 ] ; } return arr [ i1 ] [ j1 ] + arr [ i2 ] [ j2 ] ; } int maxSumPath ( int i1 , int j1 , int i2 ) { int j2 = i1 + j1 - i2 ; if ( i1 >= n i2 >= n j1 >= m j2 >= m ) { return 0 ; } if ( cache [ i1 ] [ j1 ] [ i2 ] != -1 ) { return cache [ i1 ] [ j1 ] [ i2 ] ; } int ans = INT_MIN ; ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; cache [ i1 ] [ j1 ] [ i2 ] = ans ; return ans ; } int main ( ) { memset ( cache , -1 , sizeof ( cache ) ) ; cout << maxSumPath ( 0 , 0 , 0 ) ; return 0 ; }
Bellman Ford Algorithm ( Simple Implementation ) | A C ++ program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver program to test above functions ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void BellmanFord ( int graph [ ] [ 3 ] , int V , int E , int src ) { int dis [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dis [ i ] = INT_MAX ; dis [ src ] = 0 ; for ( int i = 0 ; i < V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { if ( dis [ graph [ j ] [ 0 ] ] != INT_MAX && dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] < dis [ graph [ j ] [ 1 ] ] ) dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] ; } } for ( int i = 0 ; i < E ; i ++ ) { int x = graph [ i ] [ 0 ] ; int y = graph [ i ] [ 1 ] ; int weight = graph [ i ] [ 2 ] ; if ( dis [ x ] != INT_MAX && dis [ x ] + weight < dis [ y ] ) cout << " Graph ▁ contains ▁ negative " " ▁ weight ▁ cycle " << endl ; } cout << " Vertex ▁ Distance ▁ from ▁ Source " << endl ; for ( int i = 0 ; i < V ; i ++ ) cout << i << " TABSYMBOL TABSYMBOL " << dis [ i ] << endl ; } int main ( ) { int graph [ ] [ 3 ] = { { 0 , 1 , -1 } , { 0 , 2 , 4 } , { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 4 , 2 } , { 3 , 2 , 5 } , { 3 , 1 , 1 } , { 4 , 3 , -3 } } ; BellmanFord ( graph , V , E , 0 ) ; return 0 ; }
Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | C ++ implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Driver code ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; push_back edges
#include <bits/stdc++.h> NEW_LINE using namespace std ; int m [ 1000 ] , x [ 22 ] ; int a [ 1000 ] [ 22 ] ; vector < vector < int > > g ; int ans = 0 ; void dfs ( int u , int p ) { for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] [ i ] ; if ( v != p ) { dfs ( v , u ) ; for ( int i = 0 ; i < 22 ; i ++ ) a [ u ] [ i ] += a [ v ] [ i ] ; } } int pp = 0 ; for ( int i = 0 ; i < 22 ; i ++ ) { if ( ! ( ( a [ u ] [ i ] > 0 && x [ i ] - a [ u ] [ i ] > 0 ) || ( a [ u ] [ i ] == 0 && x [ i ] == 0 ) ) ) { pp = 1 ; break ; } } if ( pp == 0 ) ans ++ ; } int main ( ) { int n = 4 ; g . resize ( n + 1 ) ; m [ 1 ] = 1 ; m [ 2 ] = 3 ; m [ 3 ] = 2 ; m [ 4 ] = 3 ; for ( int i = 1 ; i <= n ; i ++ ) { int y = m [ i ] ; int k = 0 ; while ( y != 0 ) { int p = y % 2 ; if ( p == 1 ) { x [ k ] ++ ; a [ i ] [ k ] ++ ; } y = y / 2 ; k ++ ; } } g [ 1 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 3 ) ; g [ 3 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 4 ) ; g [ 4 ] . push_back ( 1 ) ; dfs ( 1 , 0 ) ; cout << ( ans ) ; }
Find the maximum number of composite summands of a number | C ++ implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxn = 16 ; vector < int > precompute ( ) { vector < int > dp ( maxn , -1 ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < maxn ; ++ i ) { for ( auto j : vector < int > { 4 , 6 , 9 } ) { if ( i >= j && dp [ i - j ] != -1 ) { dp [ i ] = max ( dp [ i ] , dp [ i - j ] + 1 ) ; } } } return dp ; } int Maximum_Summands ( vector < int > dp , int n ) { if ( n < maxn ) return dp [ n ] ; else { int t = ( n - maxn ) / 4 + 1 ; return t + dp [ n - 4 * t ] ; } } int main ( ) { int n = 12 ; vector < int > dp = precompute ( ) ; cout << Maximum_Summands ( dp , n ) << endl ; return 0 ; }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | C ++ implementation of the approach ; Function for returning the min of two elements ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min ( int a , int b ) { return ( a > b ) ? b : a ; } int minCostJumpsDP ( vector < int > & A , int k ) { int size = A . size ( ) ; vector < int > x ( size , INT_MAX ) ; x [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < i + k + 1 ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } int main ( ) { vector < int > input { 83 , 26 , 37 , 35 , 33 , 35 , 56 } ; cout << minCostJumpsDP ( input , 3 ) ; return 0 ; }
DP on Trees | Set | C ++ program to find diameter of a tree using DFS . ; Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; else if ( dp1 [ * i ] > secondmax ) Find secondmaximum ; Base case for every node ; if ( firstmax != - 1 ) Add ; Find dp [ 2 ] ; Return maximum of both ; Driver Code ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int diameter = -1 ; int dfs ( int node , int parent , int dp1 [ ] , int dp2 [ ] , list < int > * adj ) { int firstmax = -1 ; int secondmax = -1 ; for ( auto i = adj [ node ] . begin ( ) ; i != adj [ node ] . end ( ) ; ++ i ) { if ( * i == parent ) continue ; dfs ( * i , node , dp1 , dp2 , adj ) ; if ( firstmax == -1 ) { firstmax = dp1 [ * i ] ; } else if ( dp1 [ * i ] >= firstmax ) { secondmax = firstmax ; firstmax = dp1 [ * i ] ; } { secondmax = dp1 [ * i ] ; } } dp1 [ node ] = 1 ; dp1 [ node ] += firstmax ; if ( secondmax != -1 ) dp2 [ node ] = 1 + firstmax + secondmax ; return max ( dp1 [ node ] , dp2 [ node ] ) ; } int main ( ) { int n = 5 ; list < int > * adj = new list < int > [ n + 1 ] ; adj [ 1 ] . push_back ( 2 ) ; adj [ 2 ] . push_back ( 1 ) ; adj [ 1 ] . push_back ( 3 ) ; adj [ 3 ] . push_back ( 1 ) ; adj [ 2 ] . push_back ( 4 ) ; adj [ 4 ] . push_back ( 2 ) ; adj [ 2 ] . push_back ( 5 ) ; adj [ 5 ] . push_back ( 2 ) ; int dp1 [ n + 1 ] , dp2 [ n + 1 ] ; memset ( dp1 , 0 , sizeof dp1 ) ; memset ( dp2 , 0 , sizeof dp2 ) ; cout << " Diameter ▁ of ▁ the ▁ given ▁ tree ▁ is ▁ " << dfs ( 1 , 1 , dp1 , dp2 , adj ) << endl ; return 0 ; }
Find sub | C ++ implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE #define N  4 NEW_LINE int getSum ( int r1 , int r2 , int c1 , int c2 , int dp [ N + 1 ] [ N + 1 ] ) { return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ] ; } bool sumFound ( int K , int S , int grid [ N ] [ N ] ) { int dp [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) { int sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) return true ; } return false ; } int main ( ) { int grid [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int K = 2 ; int S = 14 ; if ( sumFound ( K , S , grid ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }