codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Manhattan distances between repetitions in a String | C ++ program for the above approach ; Function to find the sum of the manhattan distances between same characters in string ; Vector to store indices for each unique character of the string ; Append the position of each character in their respective vectors ; Store the required result ; Iterate over all the characters ; Calculate sum of all elements present in the vector ; Traverse the current vector ; Find suffix [ i + 1 ] ; Adding distance of all pairs whose first element is i and second element belongs to [ i + 1 , n ] ; Print the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SumofDistances ( string s ) { vector < int > v [ 26 ] ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { v [ s [ i ] - ' a ' ] . push_back ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) { sum += v [ i ] [ j ] ; } for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) { sum -= v [ i ] [ j ] ; ans += ( sum - ( v [ i ] . size ( ) - 1 - j ) * ( v [ i ] [ j ] ) ) ; } } cout << ans ; } int main ( ) { string s = " ababa " ; SumofDistances ( s ) ; return 0 ; }
Minimum prefixes required to be flipped to convert a Binary String to another | C ++ program for the above approach ; Function to count flips required to make strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given string ; If current characters in the two strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findOperations ( string A , string B , int N ) { int operations = 0 ; vector < int > ops ; bool invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { if ( ! invert ) { operations ++ ; ops . push_back ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . push_back ( i + 1 ) ; invert = false ; } } } cout << operations << endl ; if ( operations != 0 ) { for ( auto x : ops ) cout << x << " ▁ " ; } } int main ( ) { string A = "001" , B = "000" ; int N = A . size ( ) ; findOperations ( A , B , N ) ; return 0 ; }
Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | C ++ program for the above approach ; Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countFrequency ( string S , int N ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; int lg = log2 ( freq [ i ] ) ; int a = pow ( 2 , lg ) ; if ( a == freq [ i ] ) { while ( freq [ i ] -- ) cout << ( char ) ( i + ' a ' ) ; } } } int main ( ) { string S = " aaacbb " ; int N = S . size ( ) ; countFrequency ( S , N ) ; return 0 ; }
Longest Common Subsequence ( LCS ) by repeatedly swapping characters of a string with characters of another string | C ++ program for the above approach ; Function to find the length of LCS possible by swapping any character of a string with that of another string ; Store the size of the strings ; Stores frequency of characters ; Iterate over characters of the string A ; Update frequency of character A [ i ] ; Iterate over characters of the string B ; Update frequency of character B [ i ] ; Store the count of all pairs of similar characters ; Traverse the array freq [ ] ; Update cnt ; Print the minimum of cnt , N and M ; Driver Code ; Given strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lcsBySwapping ( string A , string B ) { int N = A . size ( ) ; int M = B . size ( ) ; int freq [ 26 ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { freq [ A [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < B . size ( ) ; i ++ ) { freq [ B [ i ] - ' a ' ] += 1 ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { cnt += freq [ i ] / 2 ; } cout << min ( cnt , min ( N , M ) ) ; } int main ( ) { string A = " abdeff " ; string B = " abbet " ; lcsBySwapping ( A , B ) ; return 0 ; }
Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | C ++ program for the above approach ; Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumLengthSubsequence ( string str ) { unordered_map < char , int > mp ; for ( char ch : str ) { mp [ ch ] ++ ; } int ans = 0 ; for ( char ch : str ) { if ( mp . count ( ch - 1 ) ) { int curr_max = mp [ ch ] + mp [ ch - 1 ] ; ans = max ( ans , curr_max ) ; } } cout << ans ; } int main ( ) { string S = " acbbebcg " ; maximumLengthSubsequence ( S ) ; return 0 ; }
Minimum increments by 1 or K required to convert a string into another given string | CPP program for the above approach ; Function to count minimum increments by 1 or K required to convert X to Y ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOperations ( string X , string Y , int K ) { int count = 0 ; for ( int i = 0 ; i < X . length ( ) ; i ++ ) { int c = 0 ; if ( X [ i ] == Y [ i ] ) continue ; else if ( X [ i ] < Y [ i ] ) { if ( ( Y [ i ] - X [ i ] ) >= K ) { c = ( Y [ i ] - X [ i ] ) / K ; } c += ( Y [ i ] - X [ i ] ) % K ; } else { int t = 90 - X [ i ] ; t += Y [ i ] - 65 + 1 ; if ( t >= K ) c = t / K ; c += ( t % K ) ; } count += c ; } cout << count << endl ; } int main ( ) { string X = " ABCT " , Y = " PBDI " ; int K = 6 ; countOperations ( X , Y , K ) ; }
Minimum number of swaps required such that a given substring consists of exactly K 1 s | CPP program for the above approach ; Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSwaps ( string s , int l , int r , int k ) { int n = s . length ( ) ; int tot_ones = 0 , tot_zeros = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) tot_ones ++ ; else tot_zeros ++ ; } int ones = 0 , zeros = 0 , sum = 0 ; for ( int i = l - 1 ; i < r ; i ++ ) { if ( s [ i ] == '1' ) { ones ++ ; sum ++ ; } else zeros ++ ; } int rem_ones = tot_ones - ones ; int rem_zeros = tot_zeros - zeros ; if ( k >= sum ) { int rem = k - sum ; if ( zeros >= rem && rem_ones >= rem ) return rem ; } else if ( k < sum ) { int rem = sum - k ; if ( ones >= rem && rem_zeros >= rem ) return rem ; } return -1 ; } int main ( ) { string S = "110011111000101" ; int L = 5 , R = 8 , K = 2 ; cout << minimumSwaps ( S , L , R , K ) ; }
Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | C ++ program for the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; vector < int > Ans ; void populateNumber ( ) { Ans . push_back ( 2 ) ; Ans . push_back ( 5 ) ; long long int x = 5 ; while ( x < 1000000000001 ) { x *= 2 ; Ans . push_back ( x ) ; x = x * 2 + 1 ; Ans . push_back ( x ) ; } } string checkString ( long long int N ) { populateNumber ( ) ; for ( auto & it : Ans ) { if ( it == N ) { return " Yes " ; } } return " No " ; } int main ( ) { long long int N = 5 ; cout << checkString ( N ) ; return 0 ; }
Minimize length by removing subsequences forming valid parenthesis from a given string | C ++ program for the above approach ; Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; Driver code ; input string ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void deleteSubseq ( string s ) { int N = s . size ( ) ; stack < char > roundStk ; stack < char > squareStk ; int roundCount = 0 ; int squareCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' [ ' ) { squareStk . push ( s [ i ] ) ; } else if ( s [ i ] == ' ] ' ) { if ( squareStk . size ( ) != 0 && squareStk . top ( ) == ' [ ' ) { squareStk . pop ( ) ; squareCount += 1 ; } } else if ( s [ i ] == ' ( ' ) { roundStk . push ( s [ i ] ) ; } else { if ( roundStk . size ( ) != 0 && squareStk . top ( ) == ' ( ' ) { squareStk . pop ( ) ; roundCount += 1 ; } } } cout << ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } int main ( ) { string s = " [ ] ] ) ( [ " ; deleteSubseq ( s ) ; }
Count minimum substring removals required to reduce string to a single distinct character | C ++ program for the above approach ; Function to find minimum removals required to convert given string to single distinct characters only ; Unordered map to store positions of characters X , Y and Z ; Update indices of X , Y , Z ; ; Stores the count of minimum removals ; Traverse the Map ; Count the number of removals required for current character ; Update the answer ; Print the answer ; Driver Code ; Given string ; Size of string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperations ( string s , int n ) { unordered_map < char , vector < int > > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ s [ i ] ] . push_back ( i ) ; } int ans = INT_MAX ; for ( auto x : mp ) { int curr = 0 ; int prev = 0 ; bool first = true ; for ( int index : ( x . second ) ) { if ( first ) { if ( index > 0 ) { curr ++ ; } prev = index ; first = false ; } else { if ( index != prev + 1 ) { curr ++ ; } prev = index ; } } if ( prev != n - 1 ) { curr ++ ; } ans = min ( ans , curr ) ; } cout << ans ; } int main ( ) { string s = " YYXYZYXYZXY " ; int N = s . length ( ) ; minimumOperations ( s , N ) ; return 0 ; }
Program to construct DFA accepting odd number of 0 s and odd number of 1 s | C ++ program for the above approach ; Function to check whether the given string is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkValidDFA ( string s ) { int initial_state = 0 ; int final_state ; int previous_state = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( s [ i ] == '0' && previous_state == 0 ) || ( s [ i ] == '1' && previous_state == 3 ) ) { final_state = 1 ; } else if ( ( s [ i ] == '0' && previous_state == 3 ) || ( s [ i ] == '1' && previous_state == 0 ) ) { final_state = 2 ; } else if ( ( s [ i ] == '0' && previous_state == 1 ) || ( s [ i ] == '1' && previous_state == 2 ) ) { final_state = 0 ; } else if ( ( s [ i ] == '0' && previous_state == 2 ) || ( s [ i ] == '1' && previous_state == 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state == 3 ) { cout << " Accepted " << endl ; } else { cout << " Not ▁ Accepted " << endl ; } } int main ( ) { string s = "010011" ; checkValidDFA ( s ) ; return 0 ; }
Difference between sums of odd level and even level nodes of a Binary Tree | CPP program to find difference between sums of odd level and even level nodes of binary tree ; tree node ; returns a new tree Node ; return difference of sums of odd level and even level ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if level no . is even or odd and accordingly update the evenSum or oddSum ; check for left child ; check for right child ; driver program ; construct a tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int evenOddLevelDifference ( Node * root ) { if ( ! root ) return 0 ; queue < Node * > q ; q . push ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( level % 2 == 0 ) evenSum += temp -> data ; else oddSum += temp -> data ; if ( temp -> left ) { q . push ( temp -> left ) ; } if ( temp -> right ) { q . push ( temp -> right ) ; } size -= 1 ; } } return ( oddSum - evenSum ) ; } int main ( ) { Node * root = newNode ( 5 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 4 ) ; root -> left -> right -> left = newNode ( 3 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 7 ) ; int result = evenOddLevelDifference ( root ) ; cout << " diffence ▁ between ▁ sums ▁ is ▁ : : ▁ " ; cout << result << endl ; return 0 ; }
Difference between sums of odd level and even level nodes of a Binary Tree | A recursive program to find difference between sum of nodes at odd level and sum at even level ; Binary Tree node ; A utility function to allocate a new tree node with given data ; The main function that return difference between odd and even level nodes ; Base case ; Difference for root is root 's data - difference for  left subtree - difference for right subtree  ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left , * right ; } ; node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = Node -> right = NULL ; return ( Node ) ; } int getLevelDiff ( node * root ) { if ( root == NULL ) return 0 ; return root -> data - getLevelDiff ( root -> left ) - getLevelDiff ( root -> right ) ; } int main ( ) { node * root = newNode ( 5 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 4 ) ; root -> left -> right -> left = newNode ( 3 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 7 ) ; cout << getLevelDiff ( root ) << " ▁ is ▁ the ▁ required ▁ difference STRNEWLINE " ; return 0 ; }
Maximum spiral sum in Binary Tree | C ++ implementation to find maximum spiral sum ; structure of a node of binary tree ; A utility function to create a new node ; function to find the maximum sum contiguous subarray . implements kadane 's algorithm ; to store the maximum value that is ending up to the current index ; to store the maximum value encountered so far ; traverse the array elements ; if max_ending_here < 0 , then it could not possibly contribute to the maximum sum further ; else add the value arr [ i ] to max_ending_here ; update max_so_far ; required maxium sum contiguous subarray value ; function to find maximum spiral sum ; if tree is empty ; Create two stacks to store alternate levels For levels from right to left ; For levels from left to right ; vector to store spiral order traversal of the binary tree ; Push first level to first stack ' s1' ; traversing tree in spiral form until there are elements in any one of the stacks ; traverse current level from s1 and push nodes of next level to s2 ; push temp - data to ' arr ' ; Note that right is pushed before left ; traverse current level from s2 and push nodes of next level to s1 ; push temp - data to ' arr ' ; Note that left is pushed before right ; required maximum spiral sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } int maxSum ( vector < int > arr , int n ) { int max_ending_here = INT_MIN ; int max_so_far = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_ending_here < 0 ) max_ending_here = arr [ i ] ; else max_ending_here += arr [ i ] ; max_so_far = max ( max_so_far , max_ending_here ) ; } return max_so_far ; } int maxSpiralSum ( Node * root ) { if ( root == NULL ) return 0 ; stack < Node * > s1 ; stack < Node * > s2 ; vector < int > arr ; s1 . push ( root ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { while ( ! s1 . empty ( ) ) { Node * temp = s1 . top ( ) ; s1 . pop ( ) ; arr . push_back ( temp -> data ) ; if ( temp -> right ) s2 . push ( temp -> right ) ; if ( temp -> left ) s2 . push ( temp -> left ) ; } while ( ! s2 . empty ( ) ) { Node * temp = s2 . top ( ) ; s2 . pop ( ) ; arr . push_back ( temp -> data ) ; if ( temp -> left ) s1 . push ( temp -> left ) ; if ( temp -> right ) s1 . push ( temp -> right ) ; } } return maxSum ( arr , arr . size ( ) ) ; } int main ( ) { Node * root = newNode ( -2 ) ; root -> left = newNode ( -3 ) ; root -> right = newNode ( 4 ) ; root -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 1 ) ; root -> right -> left = newNode ( -2 ) ; root -> right -> right = newNode ( -1 ) ; root -> left -> left -> left = newNode ( -3 ) ; root -> right -> right -> right = newNode ( 2 ) ; cout << " Maximum ▁ Spiral ▁ Sum ▁ = ▁ " << maxSpiralSum ( root ) ; return 0 ; }
Sum of all leaf nodes of binary tree | CPP program to find sum of all leaf nodes of binary tree ; struct binary tree node ; return new node ; utility function which calculates sum of all leaf nodes ; add root data to sum if root is a leaf node ; propagate recursively in left and right subtree ; driver program ; construct binary tree ; variable to store sum of leaf nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void leafSum ( Node * root , int & sum ) { if ( ! root ) return ; if ( ! root -> left && ! root -> right ) sum += root -> data ; leafSum ( root -> left , sum ) ; leafSum ( root -> right , sum ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right = newNode ( 3 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> left -> right = newNode ( 8 ) ; int sum = 0 ; leafSum ( root , sum ) ; cout << sum << endl ; return 0 ; }
Inorder Tree Traversal without recursion and without stack ! |  ; A binary tree tNode has data , a pointer to left child and a pointer to right child ; Function to traverse the binary tree without recursion and without stack ; Find the inorder predecessor of current ; Make current as the right child of its inorder predecessor ; Revert the changes made in the ' if ' part to restore the original tree i . e . , fix the right child of predecessor ; Helper function that allocates a new tNode with the given data and NULL left and right pointers . ; Driver program to test above functions ; Constructed binary tree is 1 / \ 2 3 / \ 4 5
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE struct tNode { int data ; struct tNode * left ; struct tNode * right ; } ; void MorrisTraversal ( struct tNode * root ) { struct tNode * current , * pre ; if ( root == NULL ) return ; current = root ; while ( current != NULL ) { if ( current -> left == NULL ) { printf ( " % d ▁ " , current -> data ) ; current = current -> right ; } else { pre = current -> left ; while ( pre -> right != NULL && pre -> right != current ) pre = pre -> right ; if ( pre -> right == NULL ) { pre -> right = current ; current = current -> left ; } else { pre -> right = NULL ; printf ( " % d ▁ " , current -> data ) ; current = current -> right ; } } } } struct tNode * newtNode ( int data ) { struct tNode * node = new tNode ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int main ( ) { struct tNode * root = newtNode ( 1 ) ; root -> left = newtNode ( 2 ) ; root -> right = newtNode ( 3 ) ; root -> left -> left = newtNode ( 4 ) ; root -> left -> right = newtNode ( 5 ) ; MorrisTraversal ( root ) ; return 0 ; }