codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of possible triangles | C ++ code to count the number of possible triangles using brute force approach ; Function to count all possible triangles with arr [ ] elements ; Count of triangles ; The three loops select three different values from array ; The innermost loop checks for the triangle property ; Sum of two sides is greater than the third ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfTriangles ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] > arr [ k ] && arr [ i ] + arr [ k ] > arr [ j ] && arr [ k ] + arr [ j ] > arr [ i ] ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Total ▁ number ▁ of ▁ triangles ▁ possible ▁ is ▁ " << findNumberOfTriangles ( arr , size ) ; return 0 ; }
Count the number of possible triangles | C ++ program to count number of triangles that can be formed from given array ; Following function is needed for library function qsort ( ) . Refer www . cplusplus . com / reference / clibrary / cstdlib / qsort / ; Function to count all possible triangles with arr [ ] elements ; Sort the array elements in non - decreasing order ; Initialize count of triangles ; Fix the first element . We need to run till n - 3 as the other two elements are selected from arr [ i + 1. . . n - 1 ] ; Initialize index of the rightmost third element ; Fix the second element ; Find the rightmost element which is smaller than the sum of two fixed elements The important thing to note here is , we use the previous value of k . If value of arr [ i ] + arr [ j - 1 ] was greater than arr [ k ] , then arr [ i ] + arr [ j ] must be greater than k , because the array is sorted . ; Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr [ i ] and arr [ j ] . All elements between arr [ j + 1 ] / to arr [ k - 1 ] can form a triangle with arr [ i ] and arr [ j ] . One is subtracted from k because k is incremented one extra in above while loop . k will always be greater than j . If j becomes equal to k , then above loop will increment k , because arr [ k ] + arr [ i ] is always greater than arr [ k ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int comp ( const void * a , const void * b ) { return * ( int * ) a > * ( int * ) b ; } int findNumberOfTriangles ( int arr [ ] , int n ) { qsort ( arr , n , sizeof ( arr [ 0 ] ) , comp ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j ) count += k - j - 1 ; } } return count ; } int main ( ) { int arr [ ] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Total ▁ number ▁ of ▁ triangles ▁ possible ▁ is ▁ " << findNumberOfTriangles ( arr , size ) ; return 0 ; }
Count the number of possible triangles | C ++ implementation of the above approach ; CountTriangles function ; If it is possible with a [ l ] , a [ r ] and a [ i ] then it is also possible with a [ l + 1 ] . . a [ r - 1 ] , a [ r ] and a [ i ] ; checking for more possible solutions ; if not possible check for higher values of arr [ l ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CountTriangles ( vector < int > A ) { int n = A . size ( ) ; sort ( A . begin ( ) , A . end ( ) ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else l ++ ; } } cout << " No ▁ of ▁ possible ▁ solutions : ▁ " << count ; } int main ( ) { vector < int > A = { 4 , 3 , 5 , 7 , 6 } ; CountTriangles ( A ) ; }
Maximum sum of nodes in Binary tree such that no two are adjacent | C ++ program to find maximum sum from a subset of nodes of binary tree ; A binary tree node structure ; Utility function to create a new Binary Tree node ; Declaration of methods ; method returns maximum sum possible from subtrees rooted at grandChildrens of node ' node ' ; call for children of left child only if it is not NULL ; call for children of right child only if it is not NULL ; Utility method to return maximum sum rooted at node ' node ' ; If node is already processed then return calculated value from map ; take current node value and call for all grand children ; don 't take current node value and call for all children ; choose maximum from both above calls and store that in map ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left , * right ; } ; struct node * newNode ( int data ) { struct node * temp = new struct node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int sumOfGrandChildren ( node * node ) ; int getMaxSum ( node * node ) ; int getMaxSumUtil ( node * node , map < struct node * , int > & mp ) ; int sumOfGrandChildren ( node * node , map < struct node * , int > & mp ) { int sum = 0 ; if ( node -> left ) sum += getMaxSumUtil ( node -> left -> left , mp ) + getMaxSumUtil ( node -> left -> right , mp ) ; if ( node -> right ) sum += getMaxSumUtil ( node -> right -> left , mp ) + getMaxSumUtil ( node -> right -> right , mp ) ; return sum ; } int getMaxSumUtil ( node * node , map < struct node * , int > & mp ) { if ( node == NULL ) return 0 ; if ( mp . find ( node ) != mp . end ( ) ) return mp [ node ] ; int incl = node -> data + sumOfGrandChildren ( node , mp ) ; int excl = getMaxSumUtil ( node -> left , mp ) + getMaxSumUtil ( node -> right , mp ) ; mp [ node ] = max ( incl , excl ) ; return mp [ node ] ; } int getMaxSum ( node * node ) { if ( node == NULL ) return 0 ; map < struct node * , int > mp ; return getMaxSumUtil ( node , mp ) ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 4 ) ; root -> right -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 1 ) ; cout << getMaxSum ( root ) << endl ; return 0 ; }
Flip minimum signs of array elements to get minimum sum of positive elements possible | C ++ implementation of the approach ; Function to return the minimum number of elements whose sign must be flipped to get the positive sum of array elements as close to 0 as possible ; boolean variable used for toggling between maps ; Calculate the sum of all elements of the array ; Initializing first map ( dp [ 0 ] ) with INT_MAX because for i = 0 , there are no elements in the array to flip ; Base Case ; For toggling ; Required sum is minimum non - negative So , we iterate from i = 0 to sum and find the first i where dp [ flag ^ 1 ] [ i ] != INT_MAX ; In worst case we will flip max n - 1 elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int A [ ] , int n ) { unordered_map < int , int > dp [ 2 ] ; bool flag = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += A [ i ] ; for ( int i = - sum ; i <= sum ; i ++ ) dp [ 0 ] [ i ] = INT_MAX ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = - sum ; j <= sum ; j ++ ) { dp [ flag ] [ j ] = INT_MAX ; if ( j - A [ i - 1 ] <= sum && j - A [ i - 1 ] >= - sum ) dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] ; if ( j + A [ i - 1 ] <= sum && j + A [ i - 1 ] >= - sum && dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != INT_MAX ) dp [ flag ] [ j ] = min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) ; } flag = flag ^ 1 ; } for ( int i = 0 ; i <= sum ; i ++ ) { if ( dp [ flag ^ 1 ] [ i ] != INT_MAX ) return dp [ flag ^ 1 ] [ i ] ; } return n - 1 ; } int main ( ) { int arr [ ] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << solve ( arr , n ) ; return 0 ; }
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x |
#include <bits/stdc++.h> NEW_LINE long long countPairsBruteForce ( long long X [ ] , long long Y [ ] , long long m , long long n ) { long long ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( pow ( X [ i ] , Y [ j ] ) > pow ( Y [ j ] , X [ i ] ) ) ans ++ ; return ans ; }
Maximum items that can be filled in K Knapsacks of given Capacity |  ; 2 - d array to store states of DP ; 2 - d array to store if a state has been solved ; Vector to store power of variable ' C ' . ; function to compute the states ; Base case ; Checking if a state has been solved ; Setting a state as solved ; Recurrence relation ; Returning the solved state ; Function to initialize global variables and find the initial value of ' R ' ; Resizing the variables ; Variable to store the initial value of R ; Driver Code ; Input array ; number of knapsacks and capacity ; Performing required pre - computation ; finding the required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > dp ; vector < vector < bool > > v ; vector < int > exp_c ; int FindMax ( int i , int r , int w [ ] , int n , int c , int k ) { if ( i >= n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = FindMax ( i + 1 , r , w , n , c , k ) ; for ( int j = 0 ; j < k ; j ++ ) { int x = ( r / exp_c [ j ] ) % ( c + 1 ) ; if ( x - w [ i ] >= 0 ) dp [ i ] [ r ] = max ( dp [ i ] [ r ] , w [ i ] + FindMax ( i + 1 , r - w [ i ] * exp_c [ j ] , w , n , c , k ) ) ; } return dp [ i ] [ r ] ; } int PreCompute ( int n , int c , int k ) { exp_c . resize ( k ) ; exp_c [ 0 ] = 1 ; for ( int i = 1 ; i < k ; i ++ ) { exp_c [ i ] = ( exp_c [ i - 1 ] * ( c + 1 ) ) ; } dp . resize ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] . resize ( exp_c [ k - 1 ] * ( c + 1 ) , 0 ) ; } v . resize ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { v [ i ] . resize ( exp_c [ k - 1 ] * ( c + 1 ) , 0 ) ; } int R = 0 ; for ( int i = 0 ; i < k ; i ++ ) { R += exp_c [ i ] * c ; } return R ; } int main ( ) { int w [ ] = { 3 , 8 , 9 } ; int k = 1 , c = 11 ; int n = sizeof ( w ) / sizeof ( int ) ; int r = PreCompute ( n , c , k ) ; cout << FindMax ( 0 , r , w , n , c , k ) ; return 0 ; }
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x | C ++ program to finds the number of pairs ( x , y ) in an array such that x ^ y > y ^ x ; Function to return count of pairs with x as one element of the pair . It mainly looks for all values in Y [ ] where x ^ Y [ i ] > Y [ i ] ^ x ; If x is 0 , then there cannot be any value in Y such that x ^ Y [ i ] > Y [ i ] ^ x ; If x is 1 , then the number of pais is equal to number of zeroes in Y [ ] ; Find number of elements in Y [ ] with values greater than x upper_bound ( ) gets address of first greater element in Y [ 0. . n - 1 ] ; If we have reached here , then x must be greater than 1 , increase number of pairs for y = 0 and y = 1 ; Decrease number of pairs for x = 2 and ( y = 4 or y = 3 ) ; Increase number of pairs for x = 3 and y = 2 ; Function to return count of pairs ( x , y ) such that x belongs to X [ ] , y belongs to Y [ ] and x ^ y > y ^ x ; To store counts of 0 , 1 , 2 , 3 and 4 in array Y ; Sort Y [ ] so that we can do binary search in it ; Initialize result ; Take every element of X and count pairs with it ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int x , int Y [ ] , int n , int NoOfY [ ] ) { if ( x == 0 ) return 0 ; if ( x == 1 ) return NoOfY [ 0 ] ; int * idx = upper_bound ( Y , Y + n , x ) ; int ans = ( Y + n ) - idx ; ans += ( NoOfY [ 0 ] + NoOfY [ 1 ] ) ; if ( x == 2 ) ans -= ( NoOfY [ 3 ] + NoOfY [ 4 ] ) ; if ( x == 3 ) ans += NoOfY [ 2 ] ; return ans ; } int countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int NoOfY [ 5 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) if ( Y [ i ] < 5 ) NoOfY [ Y [ i ] ] ++ ; sort ( Y , Y + n ) ; int total_pairs = 0 ; for ( int i = 0 ; i < m ; i ++ ) total_pairs += count ( X [ i ] , Y , n , NoOfY ) ; return total_pairs ; } int main ( ) { int X [ ] = { 2 , 1 , 6 } ; int Y [ ] = { 1 , 5 } ; int m = sizeof ( X ) / sizeof ( X [ 0 ] ) ; int n = sizeof ( Y ) / sizeof ( Y [ 0 ] ) ; cout << " Total ▁ pairs ▁ = ▁ " << countPairs ( X , Y , m , n ) ; return 0 ; }
Count all distinct pairs with difference equal to k | A simple program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " << countPairsWithDiffK ( arr , n , k ) ; return 0 ; }
Count all distinct pairs with difference equal to k | A sorting based program to count pairs with difference k ; Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver program
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return -1 ; } int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 , i ; sort ( arr , arr + n ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != -1 ) count ++ ; return count ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " << countPairsWithDiffK ( arr , n , k ) ; return 0 ; }
Count all distinct pairs with difference equal to k | An efficient program to count pairs with difference k when the range numbers is small ; Initialize count ; Initialize empty hashmap . ; Insert array elements to hashmap
#define MAX  100000 NEW_LINE int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; bool hashmap [ MAX ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; if ( x - k >= 0 && hashmap [ x - k ] ) count ++ ; if ( x + k < MAX && hashmap [ x + k ] ) count ++ ; hashmap [ x ] = false ; } return count ; }
Count all distinct pairs with difference equal to k | A sorting based program to count pairs with difference k ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; arr [ r ] - arr [ l ] < sum ; Driver program to test above function
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; sort ( arr , arr + n ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " << countPairsWithDiffK ( arr , n , k ) ; return 0 ; }
Sum of Bitwise | C ++ program to find sum of Bitwise - OR of all submatrices ; Function to find prefix - count for each row from right to left ; Function to create a boolean matrix set_bit which stores a 1 aTM at an index ( R , C ) if ith bit of arr [ R ] [ C ] is set . ; array to store prefix count of zeros from right to left for boolean array ; variable to store the count of submatrices with OR value 0 ; For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; stack to store elements and the count of the numbers they popped First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; variable to store the number of submatrices with all 0 s ; Function to find sum of Bitwise - OR of all submatrices ; matrix to store the status of ith bit of each element of matrix arr ; Driver Code
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; #define n  3 NEW_LINE void findPrefixCount ( int p_arr [ ] [ n ] , bool set_bit [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( set_bit [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( int ) ( ! set_bit [ i ] [ j ] ) ; } } } int matrixOrValueOne ( bool set_bit [ ] [ n ] ) { int p_arr [ n ] [ n ] = { 0 } ; findPrefixCount ( p_arr , set_bit ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; stack < pair < int , int > > q ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 and q . top ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . top ( ) . second + 1 ) * ( q . top ( ) . first - p_arr [ i ] [ j ] ) ; c += q . top ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . push ( { p_arr [ i ] [ j ] , c } ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } int sumOrMatrix ( int arr [ ] [ n ] ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { bool set_bit [ n ] [ n ] ; for ( int R = 0 ; R < n ; R ++ ) for ( int C = 0 ; C < n ; C ++ ) set_bit [ R ] [ C ] = ( ( arr [ R ] [ C ] & ( 1 << i ) ) != 0 ) ; sum += ( mul * matrixOrValueOne ( set_bit ) ) ; mul *= 2 ; } return sum ; } int main ( ) { int arr [ ] [ n ] = { { 9 , 7 , 4 } , { 8 , 9 , 2 } , { 11 , 11 , 5 } } ; cout << sumOrMatrix ( arr ) ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the total number of required sub - sets ; Variable to store total elements which on dividing by 3 give remainder 0 , 1 and 2 respectively ; Create a dp table ; Process for n states and store the sum ( mod 3 ) for 0 , 1 and 2 ; Use of MOD for large numbers ; Final answer store at dp [ n - 1 ] [ 0 ] ; Driver Program
#include <bits/stdc++.h> NEW_LINE #define MOD  1000000007 NEW_LINE #define ll  long long int NEW_LINE using namespace std ; int totalSubSets ( ll n , ll l , ll r ) { ll zero = floor ( ( double ) r / 3 ) - ceil ( ( double ) l / 3 ) + 1 ; ll one = floor ( ( double ) ( r - 1 ) / 3 ) - ceil ( ( double ) ( l - 1 ) / 3 ) + 1 ; ll two = floor ( ( double ) ( r - 2 ) / 3 ) - ceil ( ( double ) ( l - 2 ) / 3 ) + 1 ; ll dp [ n ] [ 3 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = zero ; dp [ 0 ] [ 1 ] = one ; dp [ 0 ] [ 2 ] = two ; for ( ll i = 1 ; i < n ; ++ i ) { dp [ i ] [ 0 ] = ( ( dp [ i - 1 ] [ 0 ] * zero ) + ( dp [ i - 1 ] [ 1 ] * two ) + ( dp [ i - 1 ] [ 2 ] * one ) ) % MOD ; dp [ i ] [ 1 ] = ( ( dp [ i - 1 ] [ 0 ] * one ) + ( dp [ i - 1 ] [ 1 ] * zero ) + ( dp [ i - 1 ] [ 2 ] * two ) ) % MOD ; dp [ i ] [ 2 ] = ( ( dp [ i - 1 ] [ 0 ] * two ) + ( dp [ i - 1 ] [ 1 ] * one ) + ( dp [ i - 1 ] [ 2 ] * zero ) ) % MOD ; } return dp [ n - 1 ] [ 0 ] ; } int main ( ) { ll n = 5 ; ll l = 10 ; ll r = 100 ; cout << totalSubSets ( n , l , r ) ; return 0 ; }
Maximum sum of nodes in Binary tree such that no two are adjacent | C ++ program to find maximum sum in Binary Tree such that no two nodes are adjacent . ; A binary tree node structure ; maxSumHelper function ; This node is included ( Left and right children are not included ) ; This node is excluded ( Either left or right child is included ) ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code
#include <iostream> NEW_LINE using namespace std ; class Node { public : int data ; Node * left , * right ; Node ( int data ) { this -> data = data ; left = NULL ; right = NULL ; } } ; pair < int , int > maxSumHelper ( Node * root ) { if ( root == NULL ) { pair < int , int > sum ( 0 , 0 ) ; return sum ; } pair < int , int > sum1 = maxSumHelper ( root -> left ) ; pair < int , int > sum2 = maxSumHelper ( root -> right ) ; pair < int , int > sum ; sum . first = sum1 . second + sum2 . second + root -> data ; sum . second = max ( sum1 . first , sum1 . second ) + max ( sum2 . first , sum2 . second ) ; return sum ; } int maxSum ( Node * root ) { pair < int , int > res = maxSumHelper ( root ) ; return max ( res . first , res . second ) ; } int main ( ) { Node * root = new Node ( 10 ) ; root -> left = new Node ( 1 ) ; root -> left -> left = new Node ( 2 ) ; root -> left -> left -> left = new Node ( 1 ) ; root -> left -> right = new Node ( 3 ) ; root -> left -> right -> left = new Node ( 4 ) ; root -> left -> right -> right = new Node ( 5 ) ; cout << maxSum ( root ) ; return 0 ; }
Check if a word exists in a grid or not | C ++ program to check if the word exists in the grid or not ; Function to check if a word exists in a grid starting from the first match in the grid level : index till which pattern is matched x , y : current position in 2D array ; Pattern matched ; Out of Boundary ; If grid matches with a letter while recursion ; Marking this cell as visited ; finding subpattern in 4 directions ; marking this cell as unvisited again ; else Not matching then false ; Function to check if the word exists in the grid or not ; if total characters in matrix is less then pattern lenghth ; Traverse in the grid ; If first letter matches , then recur and check ; Driver Code ; Function to check if word exists or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define r  4 NEW_LINE #define c  5 NEW_LINE bool findmatch ( char mat [ r ] , string pat , int x , int y , int nrow , int ncol , int level ) { int l = pat . length ( ) ; if ( level == l ) return true ; if ( x < 0 y < 0 x > = nrow y > = ncol ) return false ; if ( mat [ x ] [ y ] == pat [ level ] ) { char temp = mat [ x ] [ y ] ; mat [ x ] [ y ] = ' # ' ; bool res = findmatch ( mat , pat , x - 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x + 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y - 1 , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y + 1 , nrow , ncol , level + 1 ) ; mat [ x ] [ y ] = temp ; return res ; } return false ; } bool checkMatch ( char mat [ r ] , string pat , int nrow , int ncol ) { int l = pat . length ( ) ; if ( l > nrow * ncol ) return false ; for ( int i = 0 ; i < nrow ; i ++ ) { for ( int j = 0 ; j < ncol ; j ++ ) { if ( mat [ i ] [ j ] == pat [ 0 ] ) if ( findmatch ( mat , pat , i , j , nrow , ncol , 0 ) ) return true ; } } return false ; } int main ( ) { char grid [ r ] = { " axmy " , " bgdf " , " xeet " , " raks " } ; if ( checkMatch ( grid , " geeks " , r , c ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Construct an array from its pair |  ; Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArr ( int arr [ ] , int pair [ ] , int n ) { arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ; for ( int i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; } int main ( ) { int pair [ ] = { 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 } ; int n = 5 ; int arr [ n ] ; constructArr ( arr , pair , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }