codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum difference between any two elements | C ++ implementation of simple method to find minimum difference between any pair ; Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDiff ( int arr [ ] , int n ) { int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) < diff ) diff = abs ( arr [ i ] - arr [ j ] ) ; return diff ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 19 , 18 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ difference ▁ is ▁ " << findMinDiff ( arr , n ) ; return 0 ; }
Minimize removal or insertions required to make two strings equal | C ++ program for the above approach ; Function to find the longest substring in string A which is a subsequence in B ; If the indices are out of bounds ; If an already computed subproblem occurred ; Required answer if the all the characters of A and B are the same ; Required answer if there is no substring A which is a subsequence in B ; Required answer if the current character in B is skipped ; The answer for the subproblem is the maximum among the three ; Function to return the minimum strings operations required to make two strings equal ; Initialize the dp vector ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestSubstring ( int posA , int posB , string & A , string & B , bool canSkip , int n , vector < vector < vector < int > > > & dp ) { if ( posA >= n posB >= n ) { return 0 ; } if ( dp [ posA ] [ posB ] [ canSkip ] != -1 ) { return dp [ posA ] [ posB ] [ canSkip ] ; } int op1 = 0 ; int op2 = 0 ; int op3 = 0 ; if ( A [ posA ] == B [ posB ] ) { op1 = 1 + findLongestSubstring ( posA + 1 , posB + 1 , A , B , 0 , n , dp ) ; } if ( canSkip ) { op2 = findLongestSubstring ( posA + 1 , posB , A , B , canSkip , n , dp ) ; } op3 = findLongestSubstring ( posA , posB + 1 , A , B , canSkip , n , dp ) ; return dp [ posA ] [ posB ] [ canSkip ] = max ( op1 , max ( op2 , op3 ) ) ; } void minOperations ( string A , string B , int N ) { vector < vector < vector < int > > > dp ( N , vector < vector < int > > ( N , vector < int > ( 2 , -1 ) ) ) ; cout << N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ; } int main ( ) { string A = " abab " ; string B = " baba " ; int N = A . size ( ) ; minOperations ( A , B , N ) ; return 0 ; }
Count possible N | C ++ program for the above approach ; Macros for modulus ; DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the final count ; Print the total count ; Driver Code ; Initialize the dp array with - 1 ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000007 NEW_LINE int dp [ 5005 ] [ 12 ] [ 12 ] ; int findCountUtil ( int N , int maxDigit [ ] , int position = 0 , int previous = 0 , int count = 1 ) { if ( position == N ) { return 1 ; } int & ans = dp [ position ] [ previous ] [ count ] ; if ( ans != -1 ) { return ans ; } ans = 0 ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( count == 0 && previous != i ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } else if ( count != 0 ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , ( previous == i && position != 0 ) ? count - 1 : maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } } return ans ; } void findCount ( int N , int maxDigit [ ] ) { int ans = findCountUtil ( N , maxDigit ) ; cout << ans ; } int main ( ) { int N = 2 ; int maxDigit [ 10 ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; memset ( dp , -1 , sizeof ( dp ) ) ; findCount ( N , maxDigit ) ; return 0 ; }
Find minimum difference between any two elements | C ++ program to find minimum difference between any pair in an unsorted array ; Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 19 , 18 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ difference ▁ is ▁ " << findMinDiff ( arr , n ) ; return 0 ; }
Space optimization using bit manipulations | C ++ program to mark numbers as multiple of 2 or 5 ; Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int a = 2 , b = 10 ; int size = abs ( b - a ) + 1 ; int * array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; cout << " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : STRNEWLINE " ; for ( int i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) cout << i << " ▁ " ; return 0 ; }
Remove all nodes which don 't lie in any path with sum>= k |  ; A utility function to get maximum of two integers ; A Binary Tree Node ; A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Initialize left and right sums as sum from root to this node ( including this node ) ; Recursively prune left and right subtrees ; Get the maximum of left and right sums ; If maximum is smaller than k , then this node must be deleted ; A wrapper over pruneUtil ( ) ; Driver program to test above function ; k is 45
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int max ( int l , int r ) { return ( l > r ? l : r ) ; } struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } void print ( struct Node * root ) { if ( root != NULL ) { print ( root -> left ) ; printf ( " % d ▁ " , root -> data ) ; print ( root -> right ) ; } } struct Node * pruneUtil ( struct Node * root , int k , int * sum ) { if ( root == NULL ) return NULL ; int lsum = * sum + ( root -> data ) ; int rsum = lsum ; root -> left = pruneUtil ( root -> left , k , & lsum ) ; root -> right = pruneUtil ( root -> right , k , & rsum ) ; * sum = max ( lsum , rsum ) ; if ( * sum < k ) { free ( root ) ; root = NULL ; } return root ; } struct Node * prune ( struct Node * root , int k ) { int sum = 0 ; return pruneUtil ( root , k , & sum ) ; } int main ( ) { int k = 45 ; struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> left -> left -> left = newNode ( 8 ) ; root -> left -> left -> right = newNode ( 9 ) ; root -> left -> right -> left = newNode ( 12 ) ; root -> right -> right -> left = newNode ( 10 ) ; root -> right -> right -> left -> right = newNode ( 11 ) ; root -> left -> left -> right -> left = newNode ( 13 ) ; root -> left -> left -> right -> right = newNode ( 14 ) ; root -> left -> left -> right -> right -> left = newNode ( 15 ) ; printf ( " Tree ▁ before ▁ truncation STRNEWLINE " ) ; print ( root ) ; root = prune ( root , k ) ; printf ( " Tree after truncation " print ( root ) ; return 0 ; }
Tree Traversals ( Inorder , Preorder and Postorder ) | C ++ program for different tree traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Given a binary tree , print its nodes according to the " bottom - up " postorder traversal . ; first recur on left subtree ; then recur on right subtree ; now deal with the node ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Given a binary tree , print its nodes in preorder ; first print data of node ; then recur on left sutree ; now recur on right subtree ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; Node ( int data ) { this -> data = data ; left = right = NULL ; } } ; void printPostorder ( struct Node * node ) { if ( node == NULL ) return ; printPostorder ( node -> left ) ; printPostorder ( node -> right ) ; cout << node -> data << " ▁ " ; } void printInorder ( struct Node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; cout << node -> data << " ▁ " ; printInorder ( node -> right ) ; } void printPreorder ( struct Node * node ) { if ( node == NULL ) return ; cout << node -> data << " ▁ " ; printPreorder ( node -> left ) ; printPreorder ( 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 ) ; cout << " Preorder traversal of binary tree is " ; printPreorder ( root ) ; cout << " Inorder traversal of binary tree is " ; printInorder ( root ) ; cout << " Postorder traversal of binary tree is " ; printPostorder ( root ) ; return 0 ; }
Rearrange array by interchanging positions of even and odd elements in the given array | C ++ program for the above approach ; Function to replace odd elements with even elements and vice versa ; Push the first element to stack ; iterate the array and swap even and odd ; pop and swap ; print the arr [ ] ; Driven Program ; Given array arr [ ] ; Stores the length of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swapEvenOdd ( int arr [ ] , int N ) { stack < pair < int , int > > stack ; stack . push ( { 0 , arr [ 0 ] } ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( ! stack . empty ( ) ) { if ( arr [ i ] % 2 != stack . top ( ) . second % 2 ) { pair < int , int > pop = stack . top ( ) ; stack . pop ( ) ; int index = pop . first , val = pop . second ; arr [ index ] = arr [ i ] ; arr [ i ] = val ; } else stack . push ( { i , arr [ i ] } ) ; } else stack . push ( { i , arr [ i ] } ) ; } for ( int i = 0 ; i < N ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; swapEvenOdd ( arr , N ) ; return 0 ; }
Maximum subarray sum possible after removing at most K array elements | C ++ program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores overlapping subproblems of the recurrence relation ; Initialize dp [ ] [ ] to - 1 ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ ] [ ] ; Update res ; If all array elements are negative ; Update res ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  100 NEW_LINE int mxSubSum ( int i , int * arr , int j , int dp [ ] [ M ] ) { if ( i == 0 ) { return dp [ i ] [ j ] = max ( 0 , arr [ i ] ) ; } if ( dp [ i ] [ j ] != -1 ) { return dp [ i ] [ j ] ; } int X = max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j , dp ) ) ; if ( j == 0 ) { return dp [ i ] [ j ] = X ; } int Y = mxSubSum ( i - 1 , arr , j - 1 , dp ) ; return dp [ i ] [ j ] = max ( X , Y ) ; } int MaximumSubarraySum ( int n , int * arr , int k ) { int dp [ M ] [ M ] ; memset ( dp , -1 , sizeof ( dp ) ) ; mxSubSum ( n - 1 , arr , k , dp ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { res = max ( res , dp [ i ] [ j ] ) ; } } if ( * max_element ( arr , arr + n ) < 0 ) { res = * max_element ( arr , arr + n ) ; } return res ; } int main ( ) { int arr [ ] = { -2 , 1 , 3 , -2 , 4 , -7 , 20 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MaximumSubarraySum ( N , arr , K ) << endl ; return 0 ; }
Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | C ++ program for the above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Initialize the table cost [ ] [ ] ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  10000 NEW_LINE void minCost ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int m = 2 * n ; int cost [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { cost [ i ] [ j ] = INF ; } } cost [ 0 ] [ 0 ] = 0 ; int prev = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { prev = cost [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= m ; j ++ ) { cost [ i ] [ j ] = min ( cost [ i ] [ j ] , prev + abs ( j - arr [ i - 1 ] ) ) ; prev = min ( prev , cost [ i - 1 ] [ j ] ) ; } } int minCost = INF ; for ( int j = 1 ; j <= m ; j ++ ) { minCost = min ( minCost , cost [ n ] [ j ] ) ; } cout << minCost ; } int main ( ) { int arr [ ] = { 4 , 2 , 4 , 4 , 5 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minCost ( arr , N ) ; return 0 ; }
Space optimization using bit manipulations | C ++ code to for marking multiples ; index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver program to test above functions ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkbit ( int array [ ] , int index ) { return array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; } void setbit ( int array [ ] , int index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } int main ( ) { int a = 2 , b = 10 ; int size = abs ( b - a ) ; size = ceil ( size / 32 ) ; int * array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; cout << " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : STRNEWLINE " ; for ( int i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) cout << i << " ▁ " ; return 0 ; }
Longest Span with same Sum in two Binary arrays | A Simple C ++ program to find longest common subarray of two binary arrays with same sum ; Returns length of the longest common subarray with same sum ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLen ) maxLen = len ; } } } return maxLen ; } int main ( ) { bool arr1 [ ] = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; bool arr2 [ ] = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ with ▁ same ▁ " " sum ▁ is ▁ " << longestCommonSum ( arr1 , arr2 , n ) ; return 0 ; }
Number of Longest Increasing Subsequences | C ++ program for the above approach ; Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Driver code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfLIS ( vector < int > nums ) { if ( nums . size ( ) == 0 ) return 0 ; int n = nums . size ( ) ; vector < int > dp_l ( n , 1 ) ; vector < int > dp_c ( n , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } int max_length = 0 ; for ( int i : dp_l ) max_length = max ( i , max_length ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } int main ( ) { vector < int > arr = { 1 , 3 , 5 , 4 , 7 } ; cout << findNumberOfLIS ( arr ) << endl ; }
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | C ++ program for the above approach ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Driver Code ; Construct a Binary Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int val ; Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * temp = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; temp -> val = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } bool checkEvenOddLevel ( Node * root ) { if ( root == NULL ) return true ; queue < Node * > q ; q . push ( root ) ; int level = 0 ; while ( q . empty ( ) ) { vector < int > vec ; int size = q . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node * node = q . front ( ) ; vec . push_back ( node -> val ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; } if ( level % 2 == 0 ) { for ( int i = 0 ; i < vec . size ( ) - 1 ; i ++ ) { if ( vec [ i + 1 ] > vec [ i ] ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( int i = 0 ; i < vec . size ( ) - 1 ; i ++ ) { if ( vec [ i + 1 ] < vec [ i ] ) continue ; return false ; } } level ++ ; } return true ; } int main ( ) { Node * root = NULL ; root = newNode ( 2 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 7 ) ; root -> right -> right = newNode ( 11 ) ; root -> left -> left -> left = newNode ( 10 ) ; root -> left -> left -> right = newNode ( 5 ) ; root -> left -> right -> right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) cout << " YES " ; else cout << " NO " ; }
Longest Span with same Sum in two Binary arrays | C ++ program to find largest subarray with equal number of 0 ' s ▁ and ▁ 1' s . ; Returns largest common subarray with equal number of 0 s and 1 s in both of t ; Find difference between the two ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; To handle sum = 0 at last index ; If this sum is seen before , then update max_len if required ; Else put this sum in hash table ; Driver progra + m to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) { int arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr1 [ i ] - arr2 [ i ] ; unordered_map < int , int > hM ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) max_len = i + 1 ; if ( hM . find ( sum ) != hM . end ( ) ) max_len = max ( max_len , i - hM [ sum ] ) ; else hM [ sum ] = i ; } return max_len ; } int main ( ) { bool arr1 [ ] = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; bool arr2 [ ] = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << longestCommonSum ( arr1 , arr2 , n ) ; return 0 ; }
Minimum length of Run Length Encoding possible by removing at most K characters from a given string | C ++ Program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  20 NEW_LINE int dp [ maxN ] [ maxN ] [ 27 ] [ maxN ] ; int solve ( string & s , int n , int idx , int k , char last = 123 , int count = 0 ) { if ( k < 0 ) return n + 1 ; if ( idx == n ) return 0 ; int & ans = dp [ idx ] [ k ] [ last - ' a ' ] [ count ] ; if ( ans != -1 ) return ans ; ans = n + 1 ; ans = min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) ; int inc = 0 ; if ( count == 1 count == 9 count == 99 ) inc = 1 ; if ( last == s [ idx ] ) { ans = min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] , count + 1 ) ) ; } else { ans = min ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] , 1 ) ) ; } return ans ; } int MinRunLengthEncoding ( string & s , int n , int k ) { memset ( dp , -1 , sizeof ( dp ) ) ; return solve ( s , n , 0 , k ) ; } int main ( ) { string S = " abbbcdcdd " ; int N = 9 , K = 2 ; cout << MinRunLengthEncoding ( S , N , K ) ; return 0 ; }