codeparrot/xlcost-text-to-code huggingface dataset:
Maximum cost of splitting given Binary Tree into two halves | C ++ program for the above approach ; To store the results and sum of all nodes in the array ; To create adjacency list ; Function to add edges into the adjacency list ; Recursive function that calculate the value of the cost of splitting the tree recursively ; Fetch the child of node - r ; Neglect if cur node is parent ; Add all values of nodes which are decendents of r ; The two trees formed are rooted at ' r ' with its decendents ; Check and replace if current product t1 * t2 is large ; Function to find the maximum cost after splitting the tree in 2 halves ; Find sum of values in all nodes ; Traverse edges to create adjacency list ; Function Call ; Driver Code ; Values in each node ; Given Edges
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 , allsum = 0 ; vector < int > edges [ 100001 ] ; void addedge ( int a , int b ) { edges [ a ] . push_back ( b ) ; edges [ b ] . push_back ( a ) ; } void findCost ( int r , int p , int arr [ ] ) { int i , cur ; for ( i = 0 ; i < edges [ r ] . size ( ) ; i ++ ) { cur = edges [ r ] . at ( i ) ; if ( cur == p ) continue ; findCost ( cur , r , arr ) ; arr [ r ] += arr [ cur ] ; } int t1 = arr [ r ] ; int t2 = allsum - t1 ; if ( t1 * t2 > ans ) { ans = t1 * t2 ; } } void maximumCost ( int r , int p , int N , int M , int arr [ ] , int Edges [ ] [ 2 ] ) { for ( int i = 0 ; i < N ; i ++ ) { allsum += arr [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { addedge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } findCost ( r , p , arr ) ; } int main ( ) { int a , b , N = 6 ; int arr [ ] = { 13 , 8 , 7 , 4 , 5 , 9 } ; int M = 5 ; int Edges [ ] [ 2 ] = { { 0 , 1 } , { 1 , 2 } , { 1 , 4 } , { 3 , 4 } , { 4 , 5 } } ; maximumCost ( 1 , -1 , N , M , arr , Edges ) ; cout << ans ; return 0 ; }
Diameters for each node of Tree after connecting it with given disconnected component | C ++ Program to implement the above approach ; Keeps track of the farthest end of the diameter ; Keeps track of the length of the diameter ; Stores the nodes which are at ends of the diameter ; Perform DFS on the given tree ; Update diameter and X ; If current node is an end of diameter ; Traverse its neighbors ; Function to call DFS for the required purposes ; DFS from a random node and find the node farthest from it ; DFS from X to calculate diameter ; DFS from farthest_node to find the farthest node ( s ) from it ; DFS from X ( other end of diameter ) and check the farthest node ( s ) from it ; If node i is the end of a diameter ; Increase diameter by 1 ; Otherwise ; Remains unchanged ; Driver Code ; constructed tree is 1 / \ 2 3 7 / | \ / | \ 4 5 6 ; creating undirected edges
#include <bits/stdc++.h> NEW_LINE using namespace std ; int X = 1 ; int diameter = 0 ; map < int , bool > mp ; void dfs ( int current_node , int prev_node , int len , bool add_to_map , vector < vector < int > > & adj ) { if ( len > diameter ) { diameter = len ; X = current_node ; } if ( add_to_map && len == diameter ) { mp [ current_node ] = 1 ; } for ( auto & it : adj [ current_node ] ) { if ( it != prev_node ) dfs ( it , current_node , len + 1 , add_to_map , adj ) ; } } void dfsUtility ( vector < vector < int > > & adj ) { dfs ( 1 , -1 , 0 , 0 , adj ) ; int farthest_node = X ; dfs ( farthest_node , -1 , 0 , 0 , adj ) ; dfs ( farthest_node , -1 , 0 , 1 , adj ) ; dfs ( X , -1 , 0 , 1 , adj ) ; } void printDiameters ( vector < vector < int > > & adj ) { dfsUtility ( adj ) ; for ( int i = 1 ; i <= 6 ; i ++ ) { if ( mp [ i ] == 1 ) cout << diameter + 1 << " , ▁ " ; else cout << diameter << " , ▁ " ; } } int main ( ) { vector < vector < int > > adj ( 7 ) ; 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 ) ; adj [ 2 ] . push_back ( 6 ) ; adj [ 6 ] . push_back ( 2 ) ; printDiameters ( adj ) ; return 0 ; }
Count of replacements required to make the sum of all Pairs of given type from the Array equal | C ++ Program to implement the above approach ; Function to find the minimum replacements required ; Stores the maximum and minimum values for every pair of the form arr [ i ] , arr [ n - i - 1 ] ; Map for storing frequencies of every sum formed by pairs ; Minimum element in the pair ; Maximum element in the pair ; Incrementing the frequency of sum encountered ; Insert minimum and maximum values ; Sorting the vectors ; Iterate over all possible values of x ; Count of pairs for which x > x + k ; Count of pairs for which x < mn + 1 ; Count of pairs requiring 2 replacements ; Count of pairs requiring no replacements ; Count of pairs requiring 1 replacement ; Update the answer ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define int  long long int NEW_LINE using namespace std ; const int inf = 1e18 ; int minimumReplacement ( int * arr , int N , int K ) { int ans = inf ; vector < int > max_values ; vector < int > min_values ; map < int , int > sum_equal_to_x ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int mn = min ( arr [ i ] , arr [ N - i - 1 ] ) ; int mx = max ( arr [ i ] , arr [ N - i - 1 ] ) ; sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] ++ ; min_values . push_back ( mn ) ; max_values . push_back ( mx ) ; } sort ( max_values . begin ( ) , max_values . end ( ) ) ; sort ( min_values . begin ( ) , min_values . end ( ) ) ; for ( int x = 2 ; x <= 2 * K ; x ++ ) { int mp1 = lower_bound ( max_values . begin ( ) , max_values . end ( ) , x - K ) - max_values . begin ( ) ; int mp2 = lower_bound ( min_values . begin ( ) , min_values . end ( ) , x ) - min_values . begin ( ) ; int rep2 = mp1 + ( N / 2 - mp2 ) ; int rep0 = sum_equal_to_x [ x ] ; int rep1 = ( N / 2 - rep2 - rep0 ) ; ans = min ( ans , rep2 * 2 + rep1 ) ; } return ans ; } int32_t main ( ) { int N = 4 ; int K = 3 ; int arr [ ] = { 1 , 2 , 2 , 1 } ; cout << minimumReplacement ( arr , N , K ) ; return 0 ; }
Print the Forests of a Binary Tree after removal of given Nodes | C ++ Program to implement the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < int , bool > mp ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } bool deleteNode ( int nodeVal ) { return mp . find ( nodeVal ) != mp . end ( ) ; } Node * treePruning ( Node * root , vector < Node * > & result ) { if ( root == NULL ) return NULL ; root -> left = treePruning ( root -> left , result ) ; root -> right = treePruning ( root -> right , result ) ; if ( deleteNode ( root -> key ) ) { if ( root -> left ) { result . push_back ( root -> left ) ; } if ( root -> right ) { result . push_back ( root -> right ) ; } return NULL ; } return root ; } void printInorderTree ( Node * root ) { if ( root == NULL ) return ; printInorderTree ( root -> left ) ; cout << root -> key << " ▁ " ; printInorderTree ( root -> right ) ; } void printForests ( Node * root , int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] = true ; } vector < Node * > result ; if ( treePruning ( root , result ) ) result . push_back ( root ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { printInorderTree ( result [ i ] ) ; cout << endl ; } } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 12 ) ; root -> right = newNode ( 13 ) ; root -> right -> left = newNode ( 14 ) ; root -> right -> right = newNode ( 15 ) ; root -> right -> left -> left = newNode ( 21 ) ; root -> right -> left -> right = newNode ( 22 ) ; root -> right -> right -> left = newNode ( 23 ) ; root -> right -> right -> right = newNode ( 24 ) ; int arr [ ] = { 14 , 23 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printForests ( root , arr , n ) ; }
Count of Ways to obtain given Sum from the given Array elements | C ++ program to implement the above approach ; Function to call dfs to calculate the number of ways ; If target + sum is odd or S exceeds sum ; No sultion exists ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int knapSack ( int nums [ ] , int S , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += nums [ i ] ; if ( sum < S || - sum > - S || ( S + sum ) % 2 == 1 ) return 0 ; int dp [ ( S + sum ) / 2 + 1 ] ; for ( int i = 0 ; i <= ( S + sum ) / 2 ; i ++ ) dp [ i ] = 0 ; dp [ 0 ] = 1 ; for ( int j = 0 ; j < n ; j ++ ) { for ( int i = ( S + sum ) / 2 ; i >= nums [ j ] ; i -- ) { dp [ i ] += dp [ i - nums [ j ] ] ; } } return dp [ ( S + sum ) / 2 ] ; } int main ( ) { int S = 3 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int answer = knapSack ( arr , S , 5 ) ; cout << answer << endl ; }
Maximum distance between two elements whose absolute difference is K | C ++ program for the above approach ; Function that find maximum distance between two elements whose absolute difference is K ; To store the first index ; Traverse elements and find maximum distance between 2 elements whose absolute difference is K ; If this is first occurrence of element then insert its index in map ; If next element present in map then update max distance ; If previous element present in map then update max distance ; Return the answer ; Driver code ; Given array arr [ ] ; Given difference K ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDistance ( int arr [ ] , int K , int N ) { map < int , int > Map ; int maxDist = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Map . find ( arr [ i ] ) == Map . end ( ) ) Map [ arr [ i ] ] = i ; if ( Map . find ( arr [ i ] + K ) != Map . end ( ) ) { maxDist = max ( maxDist , i - Map [ arr [ i ] + K ] ) ; } if ( Map . find ( arr [ i ] - K ) != Map . end ( ) ) { maxDist = max ( maxDist , i - Map [ arr [ i ] - K ] ) ; } } if ( maxDist == 0 ) return -1 ; else return maxDist ; } int main ( ) { int arr [ ] = { 11 , 2 , 3 , 8 , 5 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << maxDistance ( arr , K , N ) ; return 0 ; }
Count of Substrings with at least K pairwise Distinct Characters having same Frequency | C ++ Program for the above approach ; Function to find the substring with K pairwise distinct characters and with same frequency ; Stores the occurrence of each character in the substring ; Length of the string ; Iterate over the string ; Set all values at each index to zero ; Stores the count of unique characters ; Moving the substring ending at j ; Calculate the index of character in frequency array ; Increment the frequency ; Update the maximum index ; Check for both the conditions ; Return the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int no_of_substring ( string s , int N ) { int fre [ 26 ] ; int str_len ; str_len = ( int ) s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < str_len ; i ++ ) { memset ( fre , 0 , sizeof ( fre ) ) ; int max_index = 0 ; int dist = 0 ; for ( int j = i ; j < str_len ; j ++ ) { int x = s [ j ] - ' a ' ; if ( fre [ x ] == 0 ) dist ++ ; fre [ x ] ++ ; max_index = max ( max_index , fre [ x ] ) ; if ( dist >= N && ( ( max_index * dist ) == ( j - i + 1 ) ) ) count ++ ; } } return count ; } int main ( ) { string s = " abhay " ; int N = 3 ; cout << no_of_substring ( s , N ) ; return 0 ; }
Count of Ordered Pairs ( X , Y ) satisfying the Equation 1 / X + 1 / Y = 1 / N | C ++ Program for the above approach ; Function to find number of ordered positive integer pairs ( x , y ) such that they satisfy the equation ; Initialize answer variable ; Iterate over all possible values of y ; For valid x and y , ( n * n ) % ( y - n ) has to be 0 ; Increment count of ordered pairs ; Print the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n ) { int ans = 0 ; for ( int y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } cout << ans ; } int main ( ) { int n = 5 ; solve ( n ) ; return 0 ; }
Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | C ++ Program to implement the above approach ; Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver Code ; Desigining the tree ; Counter counts no . of consecutive nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > adj [ 100005 ] ; int visited [ 100005 ] = { 0 } ; int ans = 0 ; void dfs ( int node , int count , int m , int arr [ ] , int k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . size ( ) == 1 && node != 1 ) { ans ++ ; } for ( auto x : adj [ node ] ) { if ( ! visited [ x ] ) { dfs ( x , count , m , arr , k ) ; } } } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 2 , 1 , 2 , 1 } ; int N = 7 , K = 2 , M = 2 ; 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 ) ; adj [ 3 ] . push_back ( 6 ) ; adj [ 6 ] . push_back ( 3 ) ; adj [ 3 ] . push_back ( 7 ) ; adj [ 7 ] . push_back ( 3 ) ; int counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Count of N | C ++ program to implement the above approach ; Function to calculate and return the reverse of the number ; Function to calculate the total count of N - digit numbers satisfying the necessary conditions ; Initialize two variables ; Calculate the sum of odd and even positions ; Check for divisibility ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long reverse ( long num ) { long rev = 0 ; while ( num > 0 ) { int r = ( int ) ( num % 10 ) ; rev = rev * 10 + r ; num /= 10 ; } return rev ; } long count ( int N , int A , int B ) { long l = ( long ) pow ( 10 , N - 1 ) , r = ( long ) pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; long ans = 0 ; for ( long i = l ; i <= r ; i ++ ) { int even_sum = 0 , odd_sum = 0 ; long itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += num % 10 ; else even_sum += num % 10 ; num /= 10 ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } int main ( ) { int N = 2 , A = 5 , B = 3 ; cout << ( count ( N , A , B ) ) ; }
Distance Traveled by Two Trains together in the same Direction | C ++ Program to find the distance traveled together by the two trains ; Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int calc_distance ( int A [ ] , int B [ ] , int n ) { int distance_traveled_A = 0 ; int distance_traveled_B = 0 ; int answer = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { distance_traveled_A += A [ i ] ; distance_traveled_B += B [ i ] ; if ( ( distance_traveled_A == distance_traveled_B ) && ( A [ i ] == B [ i ] ) ) { answer += A [ i ] ; } } return answer ; } int main ( ) { int A [ 5 ] = { 1 , 2 , 3 , 2 , 4 } ; int B [ 5 ] = { 2 , 1 , 3 , 1 , 4 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << calc_distance ( A , B , N ) ; return 0 ; }
Sum of Nodes and respective Neighbors on the path from root to a vertex V | C ++ Program to implement the above approach ; Creating Adjacency list ; Function to perform DFS traversal ; Initializing parent of each node to p ; Iterate over the children ; Function to add values of children to respective parent nodes ; Root node ; Function to return sum of values of each node in path from V to the root ; Path from node V to root node ; Driver Code ; Insert edges into the graph ; Values assigned to each vertex ; Constructing the tree using adjacency list ; Parent array ; store values in the parent array ; Add values of children to the parent ; Find sum of nodes lying in the path ; Add root node since its value is not included yet
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > constructTree ( int n , vector < vector < int > > edges ) { vector < vector < int > > adjl ( n ) ; for ( auto e : edges ) { int u = e [ 0 ] ; int v = e [ 1 ] ; adjl [ u ] . push_back ( v ) ; adjl [ v ] . push_back ( u ) ; } return adjl ; } void DFS ( vector < vector < int > > adjl , vector < int > & parent , int u , int p ) { parent [ u ] = p ; for ( int v : adjl [ u ] ) { if ( v != p ) { DFS ( adjl , parent , v , u ) ; } } } vector < int > valuesFromChildren ( vector < int > parent , vector < int > values ) { vector < int > valuesChildren ( parent . size ( ) ) ; for ( int i = 0 ; i < parent . size ( ) ; i ++ ) { if ( parent [ i ] == -1 ) continue ; else { int p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } int findSumOfValues ( int v , vector < int > parent , vector < int > valuesChildren ) { int cur_node = v ; int sum = 0 ; while ( cur_node != -1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } int main ( ) { int n = 8 ; vector < vector < int > > edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 4 , 7 } , { 3 , 6 } } ; int v = 7 ; vector < int > values = { 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 } ; vector < vector < int > > adjl = constructTree ( n , edges ) ; vector < int > parent ( n ) ; DFS ( adjl , parent , 0 , -1 ) ; vector < int > valuesChildren = valuesFromChildren ( parent , values ) ; int sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; cout << sum << endl ; }
Find a pair in Array with second largest product | C ++ program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxProduct ( int arr [ ] , int N ) { if ( N < 3 ) { return ; } int a = arr [ 0 ] , b = arr [ 1 ] ; int c = 0 , d = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a , d = b ; a = arr [ i ] , b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] , d = arr [ j ] ; } cout << c << " ▁ " << d ; } int main ( ) { int arr [ ] = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxProduct ( arr , N ) ; return 0 ; }
Count of substrings consisting of even number of vowels | C ++ program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings ; If the current character is a vowel ; Increase count ; If substring contains even number of vowels ; Increase the answer ; Print the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } void countSubstrings ( string s , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( s [ j ] ) ) { count ++ ; } if ( count % 2 == 0 ) result ++ ; } } cout << result ; } int main ( ) { int n = 5 ; string s = " abcde " ; countSubstrings ( s , n ) ; return 0 ; }
Count of substrings consisting of even number of vowels | C ++ program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } void countSubstrings ( string s , int n ) { int temp [ ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s [ i ] ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; cout << result ; } int main ( ) { int n = 5 ; string s = " abcde " ; countSubstrings ( s , n ) ; }
Find the Level of a Binary Tree with Width K | C ++ Program to implement the above approach ; Structure of a Tree node ; Utility function to create and initialize a new node ; Function returns required level of width k , if found else - 1 ; To store the node and the label and perform traversal ; Taking the last label of each level of the tree ; Check width of current level ; If the width is equal to k then return that level ; Taking the first label of each level of the tree ; If any level does not has width equal to k , return - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int findLevel ( Node * root , int k , int level ) { queue < pair < Node * , int > > qt ; qt . push ( make_pair ( root , 0 ) ) ; int count = 1 , b , a = 0 ; while ( ! qt . empty ( ) ) { pair < Node * , int > temp = qt . front ( ) ; qt . pop ( ) ; if ( count == 1 ) { b = temp . second ; } if ( ( temp . first ) -> left ) { qt . push ( make_pair ( temp . first -> left , 2 * temp . second ) ) ; } if ( temp . first -> right ) { qt . push ( make_pair ( temp . first -> right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( b - a + 1 == k ) return level ; pair < Node * , int > secondLabel = qt . front ( ) ; a = secondLabel . second ; level += 1 ; count = qt . size ( ) ; } } return -1 ; } int main ( ) { Node * root = newNode ( 5 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 2 ) ; root -> right -> right = newNode ( 8 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 3 ) ; root -> left -> right -> right = newNode ( 4 ) ; int k = 4 ; cout << findLevel ( root , k , 1 ) << endl ; return 0 ; }