codeparrot/xlcost-text-to-code huggingface dataset:
Modify Linked List by replacing each node by nearest multiple of K | C ++ program for the above approach ; Structure of node ; Function to replace the node N by the nearest multiple of K ; Traverse the Linked List ; If data is less than K ; If the data of current node is same as K ; Otherwise change the value ; Move to the next node ; Return the updated LL ; Function to print the nodes of the Linked List ; Traverse the LL ; Print the node 's data ; Driver Code ; Given Linked List
#include <iostream> NEW_LINE using namespace std ; struct node { int data ; node * next ; } ; node * EvalNearestMult ( node * N , int K ) { node * temp = N ; int t ; while ( N != NULL ) { if ( N -> data < K ) N -> data = 0 ; else { if ( N -> data == K ) N -> data = K ; else { N -> data = ( N -> data / K ) * K ; } } N = N -> next ; } return temp ; } void printList ( node * N ) { while ( N != NULL ) { cout << N -> data << " ▁ " ; N = N -> next ; } } int main ( ) { node * head = NULL ; node * second = NULL ; node * third = NULL ; head = new node ( ) ; second = new node ( ) ; third = new node ( ) ; head -> data = 3 ; head -> next = second ; second -> data = 4 ; second -> next = third ; third -> data = 8 ; third -> next = NULL ; node * t = EvalNearestMult ( head , 3 ) ; printList ( t ) ; return 0 ; }
Modify array by replacing elements with the nearest power of its previous or next element | C ++ program for the above approach ; Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Driver Code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nearestPow ( int x , int y ) { if ( y == 1 ) return 1 ; int k = log10 ( x ) / log10 ( y ) ; if ( abs ( pow ( y , k ) - x ) < abs ( pow ( y , ( k + 1 ) ) - x ) ) return pow ( y , k ) ; return pow ( y , ( k + 1 ) ) ; } void replacebyNearestPower ( vector < int > arr ) { int prev = arr [ arr . size ( ) - 1 ] ; int lastNext = arr [ 0 ] ; int next = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int temp = arr [ i ] ; if ( i == arr . size ( ) - 1 ) next = lastNext ; else next = arr [ ( i + 1 ) % arr . size ( ) ] ; int prevPow = nearestPow ( arr [ i ] , prev ) ; int nextPow = nearestPow ( arr [ i ] , next ) ; if ( abs ( arr [ i ] - prevPow ) < abs ( arr [ i ] - nextPow ) ) arr [ i ] = prevPow ; else arr [ i ] = nextPow ; prev = temp ; } for ( int i = 0 ; i < arr . size ( ) ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { vector < int > arr { 2 , 3 , 4 , 1 , 2 } ; replacebyNearestPower ( arr ) ; }
Check if sum of array can be made equal to X by removing either the first or last digits of every array element | C ++ program for the above approach ; Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool makeSumX ( int arr [ ] , int X , int S , int i , int N ) { if ( i == N ) { return S == X ; } string a = to_string ( arr [ i ] ) ; int l = stoi ( a . substr ( 0 , a . length ( ) - 1 ) ) ; int r = stoi ( a . substr ( 1 ) ) ; bool x = makeSumX ( arr , X , S + l , i + 1 , N ) ; bool y = makeSumX ( arr , X , S + r , i + 1 , N ) ; return ( x y ) ; } void Check ( int arr [ ] , int X , int N ) { if ( makeSumX ( arr , X , 0 , 0 , N ) ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int arr [ ] = { 545 , 433 , 654 , 23 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 134 ; Check ( arr , X , N ) ; return 0 ; }
Count subarrays having product equal to the power of a given Prime Number | C ++ program for the above approach ; Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int m , int y ) { int res1 = log ( y ) / log ( m ) ; double res2 = log ( y ) / log ( m ) ; return ( res1 == res2 ) ; } int numSub ( int arr [ ] , int n , int m ) { int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += ( cnt * ( cnt - 1 ) ) / 2 ; } else { cnt = 0 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 3 } ; int m = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numSub ( arr , n , m ) ; return 0 ; }
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | C ++ program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntPairs ( int arr [ ] , int N ) { int res = 0 ; int bit [ 32 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { int pos = log2 ( arr [ i ] ) ; bit [ pos ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { res += ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntPairs ( arr , N ) ; }
Minimum MEX from all subarrays of length K | C ++ program for the above approach ; Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumMEX ( int arr [ ] , int N , int K ) { set < int > s ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s . insert ( i ) ; for ( int i = 0 ; i < K ; i ++ ) s . erase ( arr [ i ] ) ; int mex = * ( s . begin ( ) ) ; for ( int i = K ; i < N ; i ++ ) { s . erase ( arr [ i ] ) ; s . insert ( arr [ i - K ] ) ; int firstElem = * ( s . begin ( ) ) ; mex = min ( mex , firstElem ) ; } cout << mex << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumMEX ( arr , N , K ) ; return 0 ; }
Count smaller elements present in the array for each array element | C ++ program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Initialize sum with 0 ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallerNumbers ( int arr [ ] , int N ) { int hash [ 100000 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; int sum = 0 ; for ( int i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { cout << "0" ; continue ; } cout << hash [ arr [ i ] - 1 ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 4 , 1 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallerNumbers ( arr , N ) ; return 0 ; }
Modulo Operations in Programming With Negative Results | C ++ 14 program to illustrate modulo operations using the above equation ; Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int truncMod ( int a , int n ) { int q = a / n ; return a - n * q ; } int main ( ) { int a , b ; a = 9 ; b = 4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << truncMod ( a , b ) << endl ; a = -9 ; b = 4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << truncMod ( a , b ) << endl ; a = 9 ; b = -4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << truncMod ( a , b ) << endl ; a = -9 ; b = -4 ; cout << a << " ▁ % ▁ " << b << " ▁ = ▁ " << truncMod ( a , b ) << endl ; }
Program for average of an array without running into overflow | C ++ program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double average ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; return ( double ) sum / N ; } double mean ( int arr [ ] , int N ) { double avg = 0 ; for ( int i = 0 ; i < N ; i ++ ) { avg += ( arr [ i ] - avg ) / ( i + 1 ) ; } return avg ; } int main ( ) { int arr [ ] = { INT_MAX , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Average ▁ by ▁ Standard ▁ method : ▁ " << fixed << setprecision ( 10 ) << average ( arr , N ) << endl ; cout << " Average ▁ by ▁ Efficient ▁ method : ▁ " << fixed << setprecision ( 10 ) << mean ( arr , N ) << endl ; return 0 ; }
Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | C ++ program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N ) { int count = 0 ; unordered_map < double , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { double val = 1.0 * arr [ i ] ; double idx = 1.0 * ( i + 1 ) ; count += mp [ val / idx ] ; mp [ val / idx ] ++ ; } cout << count ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Count ancestors with smaller value for each node of a Binary Tree | C ++ program for the above approach ; Function to add an edge between nodes u and v ; Function to perform the DFS Traversal and store parent of each node ; Store the immediate parent ; Traverse the children of the current node ; Recursively call for function dfs for the child node ; Function to count the number of ancestors with values smaller than that of the current node ; Stores the parent of each node ; Perform the DFS Traversal ; Traverse all the nodes ; Store the number of ancestors smaller than node ; Loop until parent [ node ] != - 1 ; If the condition satisfies , increment cnt by 1 ; Print the required result for the current node ; Driver Code ; Tree Formation
#include <bits/stdc++.h> NEW_LINE using namespace std ; void add_edge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } void dfs ( vector < int > & parent , vector < int > adj [ ] , int u , int par = -1 ) { parent [ u ] = par ; for ( auto child : adj [ u ] ) { if ( child != par ) dfs ( parent , adj , child , u ) ; } } void countSmallerAncestors ( vector < int > adj [ ] , int n ) { vector < int > parent ( int ( 1e5 ) , 0 ) ; dfs ( parent , adj , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int node = i ; int cnt = 0 ; while ( parent [ node ] != -1 ) { if ( parent [ node ] < i ) cnt += 1 ; node = parent [ node ] ; } cout << cnt << " ▁ " ; } } int main ( ) { int N = 6 ; vector < int > adj [ int ( 1e5 ) ] ; add_edge ( adj , 1 , 5 ) ; add_edge ( adj , 1 , 4 ) ; add_edge ( adj , 4 , 6 ) ; add_edge ( adj , 5 , 3 ) ; add_edge ( adj , 5 , 2 ) ; countSmallerAncestors ( adj , N ) ; return 0 ; }
Count subsequences having odd Bitwise XOR values from an array | C ++ program for the above approach ; Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Driver Code ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubsequences ( vector < int > A ) { int odd = 0 ; int even = 0 ; for ( int el : A ) { if ( el % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 ) cout << ( 0 ) ; else cout << ( 1 << ( A . size ( ) - 1 ) ) ; } int main ( ) { vector < int > A = { 1 , 3 , 4 } ; countSubsequences ( A ) ; }
Maximum subarray product modulo M | C ++ program for above approach ; Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Drivers Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxModProdSubarr ( int arr [ ] , int n , int M ) { int ans = 0 ; int length = n ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product = ( product * arr [ i ] ) % M ; if ( product > ans ) { ans = product ; if ( length > j - i + 1 ) { length = j - i + 1 ; } } } } cout << " Maximum ▁ subarray ▁ product ▁ is ▁ " << ans << endl ; cout << " Minimum ▁ length ▁ of ▁ the ▁ maximum ▁ product ▁ " << " subarray ▁ is ▁ " << length << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 5 ; maxModProdSubarr ( arr , N , M ) ; return 0 ; }
Number of co | C ++ program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Driver Code ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } int numOfPairs ( vector < string > arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( coprime ( stoi ( arr [ i ] ) , stoi ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } int noOfCoPrimePairs ( int N , int d1 , int d2 ) { vector < string > l ; l . push_back ( to_string ( d1 ) ) ; l . push_back ( to_string ( d2 ) ) ; sort ( l . begin ( ) , l . end ( ) ) ; if ( N < stoi ( l [ 1 ] ) ) return 0 ; vector < string > total = l ; vector < string > temp2 = l ; int flag = 0 ; vector < string > temp3 ; while ( l [ 0 ] . length ( ) < 10 ) { for ( int i = 0 ; i < l . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( stoi ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . push_back ( l [ i ] + temp2 [ j ] ) ; temp3 . push_back ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = temp3 ; vector < string > temp3 ; } int lenOfTotal = total . size ( ) ; int ans = numOfPairs ( total , lenOfTotal ) ; cout << ( ans ) ; } int main ( ) { int N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ; }
Minimize cost of placing tiles of dimensions 2 * 1 over a Matrix | C ++ program for the above approach ; Function to find the minimum cost in placing N tiles in a grid M [ ] [ ] ; Stores the minimum profit after placing i tiles ; Traverse the grid [ ] [ ] ; Update the orig_cost ; Traverse over the range [ 2 , N ] ; Place tiles horizentally or vertically ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void tile_placing ( vector < vector < int > > grid , int N ) { int dp [ N + 5 ] = { 0 } ; int orig_cost = 0 ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { orig_cost += grid [ i ] [ j ] ; } } dp [ 0 ] = 0 ; dp [ 1 ] = abs ( grid [ 0 ] [ 0 ] - grid [ 1 ] [ 0 ] ) ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = max ( dp [ i - 1 ] + abs ( grid [ 0 ] [ i - 1 ] - grid [ 1 ] [ i - 1 ] ) , dp [ i - 2 ] + abs ( grid [ 0 ] [ i - 2 ] - grid [ 0 ] [ i - 1 ] ) + abs ( grid [ 1 ] [ i - 2 ] - grid [ 1 ] [ i - 1 ] ) ) ; } cout << orig_cost - dp [ N ] ; } int32_t main ( ) { vector < vector < int > > M = { { 7 , 5 , 1 , 3 } , { 8 , 6 , 0 , 2 } } ; int N = M [ 0 ] . size ( ) ; tile_placing ( M , N ) ; return 0 ; }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C ++ program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Driver code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSplit ( int arr [ ] , int N ) { for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; for ( int i = 0 ; i <= l - 1 ; i ++ ) { lsum += arr [ i ] ; } for ( int i = l + 1 ; i <= r - 1 ; i ++ ) { msum += arr [ i ] ; } for ( int i = r + 1 ; i < N ; i ++ ) { rsum += arr [ i ] ; } if ( lsum == rsum && rsum == msum ) { cout << l << " ▁ " << r << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { int arr [ ] = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSplit ( arr , N ) ; return 0 ; }
Count the number of times a Bulb switches its state | C ++ program for the above approach ; Function to find the number of times a bulb switches its state ; Count of 1 s ; Traverse the array ; Update count of 1 s ; Update the status of bulb ; Traverse the array Q [ ] ; Stores previous state of the bulb ; Toggle the switch and update count of 1 s ; If the bulb switches state ; Return count ; Driver Code ; Input ; Queries ; Function call to find number of times the bulb toggles
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int A [ ] , int n , int Q [ ] , int q ) { int one = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] == 1 ) one ++ ; int glows = 0 , count = 0 ; if ( one >= ceil ( n / 2 ) ) glows = 1 ; for ( int i = 0 ; i < q ; i ++ ) { int prev = glows ; if ( A [ Q [ i ] - 1 ] == 1 ) one -- ; if ( A [ Q [ i ] - 1 ] == 0 ) one ++ ; A [ Q [ i ] - 1 ] ^= 1 ; if ( one >= ceil ( n / 2.0 ) ) { glows = 1 ; } else { glows = 0 ; } if ( prev != glows ) count ++ ; } return count ; } int main ( ) { int n = 3 ; int arr [ ] = { 1 , 1 , 0 } ; int q = 3 ; int Q [ ] = { 3 , 2 , 1 } ; cout << solve ( arr , n , Q , q ) ; return 0 ; }
Count array elements having sum of digits equal to K | C ++ program for the above approach ; Function to calculate the sum of digits of the number N ; Stores the sum of digits ; Return the sum ; Function to count array elements ; Store the count of array elements having sum of digits K ; Traverse the array ; If sum of digits is equal to K ; Increment the count ; Print the count ; Driver Code ; Given array ; Given value of K ; Size of the array ; Function call to count array elements having sum of digits equal to K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigits ( int N ) { int sum = 0 ; while ( N != 0 ) { sum += N % 10 ; N /= 10 ; } return sum ; } int elementsHavingDigitSumK ( int arr [ ] , int N , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( sumOfDigits ( arr [ i ] ) == K ) { count ++ ; } } cout << count ; } int main ( ) { int arr [ ] = { 23 , 54 , 87 , 29 , 92 , 62 } ; int K = 11 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; elementsHavingDigitSumK ( arr , N , K ) ; return 0 ; }
Postfix to Infix | CPP program to find infix for a given postfix . ; Get Infix for a given postfix expression ; Push operands ; We assume that input is a valid postfix and expect an operator . ; There must be a single element in stack now which is the required infix . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOperand ( char x ) { return ( x >= ' a ' && x <= ' z ' ) || ( x >= ' A ' && x <= ' Z ' ) ; } string getInfix ( string exp ) { stack < string > s ; for ( int i = 0 ; exp [ i ] != ' \0' ; i ++ ) { if ( isOperand ( exp [ i ] ) ) { string op ( 1 , exp [ i ] ) ; s . push ( op ) ; } else { string op1 = s . top ( ) ; s . pop ( ) ; string op2 = s . top ( ) ; s . pop ( ) ; s . push ( " ( " + op2 + exp [ i ] + op1 + " ) " ) ; } } return s . top ( ) ; } int main ( ) { string exp = " ab * c + " ; cout << getInfix ( exp ) ; return 0 ; }
Change a Binary Tree so that every node stores sum of all nodes in left subtree | C ++ program to store sum of nodes in left subtree in every node ; A tree node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Function to modify a Binary Tree so that every node stores sum of values in its left child including its own value ; Base cases ; Update left and right subtrees ; Add leftsum to current node ; Return sum of values under root ; Utility function to do inorder traversal ; Driver code ; Let us construct below tree 1 / \ 2 3 / \ \ 4 5 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left , * right ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; int updatetree ( node * root ) { if ( ! root ) return 0 ; if ( root -> left == NULL && root -> right == NULL ) return root -> data ; int leftsum = updatetree ( root -> left ) ; int rightsum = updatetree ( root -> right ) ; root -> data += leftsum ; return root -> data + rightsum ; } void inorder ( node * node ) { if ( node == NULL ) return ; inorder ( node -> left ) ; cout << node -> data << " ▁ " ; inorder ( node -> right ) ; } int main ( ) { struct node * root = new node ( 1 ) ; root -> left = new node ( 2 ) ; root -> right = new node ( 3 ) ; root -> left -> left = new node ( 4 ) ; root -> left -> right = new node ( 5 ) ; root -> right -> right = new node ( 6 ) ; updatetree ( root ) ; cout << " Inorder ▁ traversal ▁ of ▁ the ▁ modified ▁ tree ▁ is : ▁ STRNEWLINE " ; inorder ( root ) ; return 0 ; }