codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum steps required to reach the end of a matrix | Set | C ++ program to implement above approach ; 2d array to store states of dp ; array to determine whether a state has been solved before ; Function to find the minimum number of steps to reach the end of matrix ; base cases ; if a state has been solved before it won 't be evaluated again. ; recurrence relation ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define n  3 NEW_LINE using namespace std ; int dp [ n ] [ n ] ; int v [ n ] [ n ] ; int minSteps ( int i , int j , int arr [ ] [ n ] ) { if ( i == n - 1 and j == n - 1 ) return 0 ; if ( i > n - 1 j > n - 1 ) return 9999999 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = 1 ; dp [ i ] [ j ] = 1 + min ( minSteps ( i + arr [ i ] [ j ] , j , arr ) , minSteps ( i , j + arr [ i ] [ j ] , arr ) ) ; return dp [ i ] [ j ] ; } int main ( ) { int arr [ n ] [ n ] = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int ans = minSteps ( 0 , 0 , arr ) ; if ( ans >= 9999999 ) cout << -1 ; else cout << ans ; return 0 ; }
Treasure and Cities | A memoization based program to find maximum treasure that can be collected . ; k is current index and col is previous color . ; if ( k == n ) base case ; if ( col == color [ k ] ) check if color of this city is equal to prev visited city ; return max of both options ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1001 ; int dp [ MAX ] [ MAX ] ; int MaxProfit ( int treasure [ ] , int color [ ] , int n , int k , int col , int A , int B ) { return dp [ k ] [ col ] = 0 ; if ( dp [ k ] [ col ] != -1 ) return dp [ k ] [ col ] ; int sum = 0 ; sum += max ( A * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; else sum += max ( B * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; return dp [ k ] [ col ] = sum ; } int main ( ) { int A = -5 , B = 7 ; int treasure [ ] = { 4 , 8 , 2 , 9 } ; int color [ ] = { 2 , 2 , 6 , 2 } ; int n = sizeof ( color ) / sizeof ( color [ 0 ] ) ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << MaxProfit ( treasure , color , n , 0 , 0 , A , B ) ; return 0 ; }
Tetranacci Numbers | A DP based CPP program to print the nth tetranacci number ; Function to print the N - th tetranacci number ; base cases ; Driver code
#include <iostream> NEW_LINE using namespace std ; int printTetra ( int n ) { int dp [ n + 5 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = dp [ 2 ] = 1 ; dp [ 3 ] = 2 ; for ( int i = 4 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] + dp [ i - 4 ] ; cout << dp [ n ] ; } int main ( ) { int n = 10 ; printTetra ( n ) ; return 0 ; }
Maximum sum in circular array such that no two elements are adjacent | CPP program to find maximum sum in a circular array such that no elements are adjacent in the sum . ; Function to calculate the sum from 0 th position to ( n - 2 ) th position ; copy the element of original array to dp [ ] ; find the maximum element in the array ; start from 2 nd to n - 1 th pos ; traverse for all pairs bottom - up approach ; dp - condition ; find maximum sum ; return the maximum ; Function to find the maximum sum from 1 st position to n - 1 - th position ; Traverse from third to n - th pos ; bootom - up approach ; dp condition ; find max sum ; return max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum1 ( int arr [ ] , int n ) { int dp [ n ] ; int maxi = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 2 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < dp [ j ] + arr [ i ] ) { dp [ i ] = dp [ j ] + arr [ i ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } int maxSum2 ( int arr [ ] , int n ) { int dp [ n ] ; int maxi = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 3 ; i < n ; i ++ ) { for ( int j = 1 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < arr [ i ] + dp [ j ] ) { dp [ i ] = arr [ i ] + dp [ j ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } int findMaxSum ( int arr [ ] , int n ) { return max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxSum ( arr , n ) ; return 0 ; }
Permutation Coefficient | A O ( n ) solution that uses table fact [ ] to calculate the Permutation Coefficient ; Returns value of Permutation Coefficient P ( n , k ) ; Base case ; Calculate value factorials up to n ; P ( n , k ) = n ! / ( n - k ) ! ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int permutationCoeff ( int n , int k ) { int fact [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; return fact [ n ] / fact [ n - k ] ; } int main ( ) { int n = 10 , k = 2 ; cout << " Value ▁ of ▁ P ( " << n << " , ▁ " << k << " ) ▁ is ▁ " << permutationCoeff ( n , k ) ; return 0 ; }
Dynamic Programming | A recursive solution for subset sum problem ; Returns true if there is a subset of set [ ] with sum equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following : ( a ) including the last element ( b ) excluding the last element ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } int main ( ) { int set [ ] = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = sizeof ( set ) / sizeof ( set [ 0 ] ) ; if ( isSubsetSum ( set , n , sum ) == true ) cout << " Found ▁ a ▁ subset ▁ with ▁ given ▁ sum " ; else cout << " No ▁ subset ▁ with ▁ given ▁ sum " ; return 0 ; }
Count of cyclic permutations having XOR with other binary string as 0 | C ++ program to find bitwise XOR between binary string A and all the cyclic permutations of binary string B ; Implementation of Z - algorithm for linear time pattern searching ; Function to get the count of the cyclic permutations of b that given 0 when XORed with a ; concatenate b with b ; new b now contains all the cyclic permutations of old b as it 's sub-strings ; concatenate pattern with text ; Fill z array used in Z algorithm ; pattern occurs at index i since z value of i equals pattern length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void compute_z ( string s , int z [ ] ) { int l = 0 , r = 0 ; int n = s . length ( ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { if ( i > r ) { l = i , r = i ; while ( r < n && s [ r - l ] == s [ r ] ) r ++ ; z [ i ] = r - l ; r -- ; } else { int k = i - l ; if ( z [ k ] < r - i + 1 ) { z [ i ] = z [ k ] ; } else { l = i ; while ( r < n && s [ r - l ] == s [ r ] ) r ++ ; z [ i ] = r - l ; r -- ; } } } } int countPermutation ( string a , string b ) { b = b + b ; b = b . substr ( 0 , b . size ( ) - 1 ) ; int ans = 0 ; string s = a + " $ " + b ; int n = s . length ( ) ; int z [ n ] ; compute_z ( s , z ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { if ( z [ i ] == a . length ( ) ) ans ++ ; } return ans ; } int main ( ) { string a = "101" ; string b = "101" ; cout << countPermutation ( a , b ) << endl ; return 0 ; }
Lexicographically smallest K | C ++ program for the above approach ; Function to find lexicographically smallest subsequence of size K ; Length of string ; Stores the minimum subsequence ; Traverse the string S ; If the stack is empty ; Iterate till the current character is less than the the character at the top of stack ; Check if there are enough characters remaining to obtain length K ; If stack size is < K ; Push the current character into it ; Stores the resultant string ; Iterate until stack is empty ; Reverse the string ; Print the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestSubsequence ( string & S , int K ) { int N = S . size ( ) ; stack < char > answer ; for ( int i = 0 ; i < N ; ++ i ) { if ( answer . empty ( ) ) { answer . push ( S [ i ] ) ; } else { while ( ( ! answer . empty ( ) ) && ( S [ i ] < answer . top ( ) ) && ( answer . size ( ) - 1 + N - i >= K ) ) { answer . pop ( ) ; } if ( answer . empty ( ) || answer . size ( ) < K ) { answer . push ( S [ i ] ) ; } } } string ret ; while ( ! answer . empty ( ) ) { ret . push_back ( answer . top ( ) ) ; answer . pop ( ) ; } reverse ( ret . begin ( ) , ret . end ( ) ) ; cout << ret ; } int main ( ) { string S = " aabdaabc " ; int K = 3 ; smallestSubsequence ( S , K ) ; return 0 ; }
Check if string is right to left diagonal or not | C ++ program to Check if the given string is right to left diagonal or not ; Function to check if the given string is right to left diagonal or not ; Iterate over string ; If character is not same as the first character then return false ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int is_rtol ( string s ) { int tmp = sqrt ( s . length ( ) ) - 1 ; char first = s [ tmp ] ; for ( int pos = tmp ; pos < s . length ( ) - 1 ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } } return true ; } int main ( ) { string str = " abcxabxcaxbcxabc " ; if ( is_rtol ( str ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }
Split a given string into substrings of length K with equal sum of ASCII values | C ++ program to check if a given string can be split into substrings of size K having an equal sum of ASCII values . ; Function for checking string ; Check if the string can be split into substrings of K length only ; Compute the sum of first substring of length K ; Compute the sum of remaining substrings ; Check if sum is equal to that of the first substring ; Since all sums are not equal , return false ; All sums are equal , Return true ; All substrings cannot be of size K ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string str , int K ) { if ( str . size ( ) % K == 0 ) { int sum = 0 , i ; for ( i = 0 ; i < K ; i ++ ) { sum += str [ i ] ; } for ( int j = i ; j < str . size ( ) ; j += K ) { int s_comp = 0 ; for ( int p = j ; p < j + K ; p ++ ) s_comp += str [ p ] ; if ( s_comp != sum ) return false ; } return true ; } return false ; } int main ( ) { int K = 3 ; string str = " abdcbbdba " ; if ( check ( str , K ) ) cout << " YES " << endl ; else cout << " NO " << endl ; }
Maximize count of 0 s in left and 1 s in right substring by splitting given Binary string | C ++ program for the above approach ; Function to maximize the sum of the count of zeros and ones in the left and right substring ; To store the total ones ; Count the total numbers of ones in string str ; To store the count of zeros and ones while traversing string ; Iterate the given string and update the maximum sum ; Update the maximum Sum ; Driver Code ; Given binary string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( string & str ) { int maximumSum = 0 ; int totalOnes ; totalOnes = count ( str . begin ( ) , str . end ( ) , '1' ) ; int zero = 0 , ones = 0 ; for ( int i = 0 ; str [ i ] ; i ++ ) { if ( str [ i ] == '0' ) { zero ++ ; } else { ones ++ ; } maximumSum = max ( maximumSum , zero + ( totalOnes - ones ) ) ; } return maximumSum ; } int main ( ) { string str = "011101" ; cout << maxSum ( str ) ; return 0 ; }
Longest substring such that no three consecutive characters are same | C ++ implementation of the approach ; Function to return the length of the longest substring such that no three consecutive characters are same ; If the length of the given string is less than 3 ; Initialize temporary and final ans to 2 as this is the minimum length of substring when length of the given string is greater than 2 ; Traverse the string from the third character to the last ; If no three consecutive characters are same then increment temporary count ; Else update the final ans and reset the temporary count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLenSubStr ( string & s ) { if ( s . length ( ) < 3 ) return s . length ( ) ; int temp = 2 ; int ans = 2 ; for ( int i = 2 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] != s [ i - 1 ] s [ i ] != s [ i - 2 ] ) temp ++ ; else { ans = max ( temp , ans ) ; temp = 2 ; } } ans = max ( temp , ans ) ; return ans ; } int main ( ) { string s = " baaabbabbb " ; cout << maxLenSubStr ( s ) ; return 0 ; }
Number of ways to remove a sub | C ++ program to count number of ways of removing a substring from a string such that all remaining characters are equal ; Function to return the number of ways of removing a sub - string from s such that all the remaining characters are same ; To store the count of prefix and suffix ; Loop to count prefix ; Loop to count suffix ; First and last characters of the string are same ; Otherwise ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int no_of_ways ( string s ) { int n = s . length ( ) ; int count_left = 0 , count_right = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == s [ 0 ] ) { ++ count_left ; } else break ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == s [ n - 1 ] ) { ++ count_right ; } else break ; } if ( s [ 0 ] == s [ n - 1 ] ) return ( ( count_left + 1 ) * ( count_right + 1 ) ) ; else return ( count_left + count_right + 1 ) ; } int main ( ) { string s = " geeksforgeeks " ; cout << no_of_ways ( s ) ; return 0 ; }
Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | C ++ program to find substring with ; Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; void preCompute ( int n , string s , int pref [ ] ) { pref [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } int query ( int pref [ ] , int l , int r ) { return pref [ r ] - pref [ l ] ; } int main ( ) { string s = " ggggggg " ; int n = s . length ( ) ; int pref [ n ] ; preCompute ( n , s , pref ) ; int l = 1 ; int r = 2 ; cout << query ( pref , l , r ) << endl ; l = 1 ; r = 5 ; cout << query ( pref , l , r ) << endl ; return 0 ; }
Find the direction from given string | CPP implementation of above approach ; Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findDirection ( string s ) { int count = 0 ; string d = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ 0 ] == ' ' ) return NULL ; if ( s [ i ] == ' L ' ) count -- ; else { if ( s [ i ] == ' R ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == 1 ) d = " E " ; else if ( count % 4 == 2 ) d = " S " ; else if ( count % 4 == 3 ) d = " W " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == -1 ) d = " W " ; else if ( count % 4 == -2 ) d = " S " ; else if ( count % 4 == -3 ) d = " E " ; } return d ; } int main ( ) { string s = " LLRLRRL " ; cout << ( findDirection ( s ) ) << endl ; s = " LL " ; cout << ( findDirection ( s ) ) << endl ; }
Check if lowercase and uppercase characters are in same order | C ++ program to check if lowercase and uppercase characters are in same order ; Function to check if both the case follow the same order ; Traverse the string ; Store both lowercase and uppercase in two different strings ; using transform ( ) function and :: toupper in STL ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCheck ( string str ) { int len = str . length ( ) ; string lowerStr = " " , upperStr = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] >= 65 && str [ i ] <= 91 ) upperStr = upperStr + str [ i ] ; else lowerStr = lowerStr + str [ i ] ; } transform ( lowerStr . begin ( ) , lowerStr . end ( ) , lowerStr . begin ( ) , :: toupper ) ; return lowerStr == upperStr ; } int main ( ) { string str = " geeGkEEsKS " ; isCheck ( str ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Replace every character of string by character whose ASCII value is K times more than it | CPP program to move every character K times ahead in a given string ; Function to move string character ; changed string ; iterate for every characters ; ASCII value ; store the duplicate ; if k - th ahead character exceed ' z ' ; print the new string ; driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void encode ( string s , int k ) { string newS ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { int val = int ( s [ i ] ) ; int dup = k ; if ( val + k > 122 ) { k -= ( 122 - val ) ; k = k % 26 ; newS += char ( 96 + k ) ; } else newS += char ( val + k ) ; k = dup ; } cout << newS ; } int main ( ) { string str = " abc " ; int k = 28 ; encode ( str , k ) ; return 0 ; }
Modify string by removing vowels in between two consonants | C ++ program to remove all Vowels in between two consonants from the string ; Function to check if the character x is a vowel or not ; Returns the updated string formed after removing all the Sandwiched Vowels from the given string ; string to store the Updated String after removing the Sandwiched Vowels ; traverse the string from left to right ; if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched ' ; Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string ; if this character is not a sandwiched Vowel append it to the updated String ; Driver Code ; Remove all the Sandwitched Vowels
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char x ) { if ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) return true ; else return false ; } string updateSandwichedVowels ( string a ) { int n = a . length ( ) ; string updatedString = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! i i == n - 1 ) { updatedString += a [ i ] ; continue ; } if ( isVowel ( a [ i ] ) && ! isVowel ( a [ i - 1 ] ) && ! isVowel ( a [ i + 1 ] ) ) { continue ; } updatedString += a [ i ] ; } return updatedString ; } int main ( ) { string str = " geeksforgeeks " ; string updatedString = updateSandwichedVowels ( str ) ; cout << updatedString ; return 0 ; }
Print all paths of the Binary Tree with maximum element in each path greater than or equal to K | C ++ program to print paths with maximum element in the path greater than K ; A Binary Tree node ; A utility function to create a new node ; A recursive function to print the paths whose maximum element is greater than or equal to K . ; If the current node value is greater than or equal to k , then all the subtrees following that node will get printed , flag = 1 indicates to print the required path ; If the leaf node is encountered , then the path is printed if the size of the path vector is greater than 0 ; Append the node to the path vector ; Recur left and right subtrees ; Backtracking to return the vector and print the path if the flag is 1 ; Function to initialize the variables and call the utility function to print the paths with maximum values greater than or equal to K ; Initialize flag ; ans is used to check empty condition ; Call function that print path ; If the path doesn 't exist ; Driver code ; Constructing the following tree : 10 / \ 5 8 / \ / \ 29 2 1 98 / \ 20 50
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return ( newNode ) ; } void findPathUtil ( Node * root , int k , vector < int > path , int flag , int & ans ) { if ( root == NULL ) return ; if ( root -> data >= k ) flag = 1 ; if ( root -> left == NULL && root -> right == NULL ) { if ( flag == 1 ) { ans = 1 ; cout << " ( " ; for ( int i = 0 ; i < path . size ( ) ; i ++ ) { cout << path [ i ] << " , ▁ " ; } cout << root -> data << " ) , ▁ " ; } return ; } path . push_back ( root -> data ) ; findPathUtil ( root -> left , k , path , flag , ans ) ; findPathUtil ( root -> right , k , path , flag , ans ) ; path . pop_back ( ) ; } void findPath ( Node * root , int k ) { int flag = 0 ; int ans = 0 ; vector < int > v ; findPathUtil ( root , k , v , flag , ans ) ; if ( ans == 0 ) cout << " - 1" ; } int main ( void ) { int K = 25 ; struct Node * root = newNode ( 10 ) ; root -> left = newNode ( 5 ) ; root -> right = newNode ( 8 ) ; root -> left -> left = newNode ( 29 ) ; root -> left -> right = newNode ( 2 ) ; root -> right -> right = newNode ( 98 ) ; root -> right -> left = newNode ( 1 ) ; root -> right -> right -> right = newNode ( 50 ) ; root -> left -> left -> left = newNode ( 20 ) ; findPath ( root , K ) ; return 0 ; }
Tridecagonal Number | C ++ program to find N - th Tridecagonal number ; Function to find N - th Tridecagonal number ; Formula to calculate nth Tridecagonal number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Tridecagonal_num ( int n ) { return ( 11 * n * n - 9 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << Tridecagonal_num ( n ) << endl ; n = 10 ; cout << Tridecagonal_num ( n ) << endl ; return 0 ; }
Check if a given Binary Tree is Heap | C ++ program to checks if a binary tree is max heap or not ; Tree node structure ; To add a new node ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int k ) { struct Node * node = new Node ; node -> data = k ; node -> right = node -> left = NULL ; return node ; } bool isHeap ( Node * root ) { queue < Node * > q ; q . push ( root ) ; bool nullish = false ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left ) { if ( nullish temp -> left -> data > = temp -> data ) { return false ; } q . push ( temp -> left ) ; } else { nullish = true ; } if ( temp -> right ) { if ( nullish temp -> right -> data > = temp -> data ) { return false ; } q . push ( temp -> right ) ; } else { nullish = true ; } } return true ; } int main ( ) { struct Node * root = NULL ; root = newNode ( 10 ) ; root -> left = newNode ( 9 ) ; root -> right = newNode ( 8 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 4 ) ; root -> left -> left -> left = newNode ( 3 ) ; root -> left -> left -> right = newNode ( 2 ) ; root -> left -> right -> left = newNode ( 1 ) ; if ( isHeap ( root ) ) cout << " Given ▁ binary ▁ tree ▁ is ▁ a ▁ Heap STRNEWLINE " ; else cout << " Given ▁ binary ▁ tree ▁ is ▁ not ▁ a ▁ Heap STRNEWLINE " ; return 0 ; }
Number of N digit integers with weight W | CPP program to find total possible numbers with n digits and weight w ; Function to find total possible numbers with n digits and weight w ; When Weight of an integer is Positive ; Subtract the weight from 9 ; When weight of an integer is negative ; add the weight to 10 to make it positive ; Driver code ; number of digits in an integer and w as weight ; print the total possible numbers with n digits and weight w
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; int findNumbers ( int n , int w ) { int x = 0 , sum = 0 ; if ( w >= 0 && w <= 8 ) { x = 9 - w ; } else if ( w >= -9 && w <= -1 ) { x = 10 + w ; } sum = pow ( 10 , n - 2 ) ; sum = ( x * sum ) ; return sum ; } int main ( ) { int n , w ; n = 3 , w = 4 ; cout << findNumbers ( n , w ) ; ; return 0 ; }