codeparrot/xlcost-text-to-code huggingface dataset:
Find the minimum number of moves needed to move from one cell of matrix to another | C ++ program to find the minimum numbers of moves needed to move from source to destination . ; add edge to graph ; Level BFS function to find minimum path from source to sink ; Base case ; make initial distance of all vertex - 1 from source ; Create a queue for BFS ; Mark the source node level [ s ] = '0' ; it will be used to get all adjacent vertices of a vertex ; Dequeue a vertex from queue ; Get all adjacent vertices of the dequeued vertex s . If a adjacent has not been visited ( level [ i ] < '0' ) , then update level [ i ] = = parent_level [ s ] + 1 and enqueue it ; Else , continue to do BFS ; return minimum moves from source to sink ; Returns minimum numbers of moves from a source ( acell with value 1 ) to a destination ( a cell withvalue 2 ) ;  ; create graph with n * n node each cell consider as node Number of current vertex ; connect all 4 adjacent cell to current cell ; source index ; destination index ; find minimum moves ; driver program to check above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE class Graph { int V ; list < int > * adj ; public : Graph ( int V ) { this -> V = V ; adj = new list < int > [ V ] ; } void addEdge ( int s , int d ) ; int BFS ( int s , int d ) ; } ; void Graph :: addEdge ( int s , int d ) { adj [ s ] . push_back ( d ) ; adj [ d ] . push_back ( s ) ; } int Graph :: BFS ( int s , int d ) { if ( s == d ) return 0 ; int * level = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) level [ i ] = -1 ; list < int > queue ; level [ s ] = 0 ; queue . push_back ( s ) ; list < int > :: iterator i ; while ( ! queue . empty ( ) ) { s = queue . front ( ) ; queue . pop_front ( ) ; for ( i = adj [ s ] . begin ( ) ; i != adj [ s ] . end ( ) ; ++ i ) { if ( level [ * i ] < 0 level [ * i ] > level [ s ] + 1 ) { level [ * i ] = level [ s ] + 1 ; queue . push_back ( * i ) ; } } } return level [ d ] ; } bool isSafe ( int i , int j , int M [ ] [ N ] ) { if ( ( i < 0 i > = N ) || ( j < 0 j > = N ) M [ i ] [ j ] == 0 ) return false ; return true ; } int MinimumPath ( int M [ ] [ N ] ) { int s , d ; int V = N * N + 2 ; Graph g ( V ) ; int k = 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( M [ i ] [ j ] != 0 ) { if ( isSafe ( i , j + 1 , M ) ) g . addEdge ( k , k + 1 ) ; if ( isSafe ( i , j - 1 , M ) ) g . addEdge ( k , k - 1 ) ; if ( j < N - 1 && isSafe ( i + 1 , j , M ) ) g . addEdge ( k , k + N ) ; if ( i > 0 && isSafe ( i - 1 , j , M ) ) g . addEdge ( k , k - N ) ; } if ( M [ i ] [ j ] == 1 ) s = k ; if ( M [ i ] [ j ] == 2 ) d = k ; k ++ ; } } return g . BFS ( s , d ) ; } int main ( ) { int M [ N ] [ N ] = { { 3 , 3 , 1 , 0 } , { 3 , 0 , 3 , 3 } , { 2 , 3 , 0 , 3 } , { 0 , 3 , 3 , 3 } } ; cout << MinimumPath ( M ) << endl ; return 0 ; }
Find two numbers whose sum and GCD are given | C ++ program to find two numbers whose sum and GCD is given ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTwoNumbers ( int sum , int gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) cout << " a ▁ = ▁ " << min ( gcd , sum - gcd ) << " , ▁ b ▁ = ▁ " << sum - min ( gcd , sum - gcd ) << endl ; else cout << -1 << endl ; } int main ( ) { int sum = 8 ; int gcd = 2 ; findTwoNumbers ( sum , gcd ) ; return 0 ; }
Find maximum distance between any city and station | C ++ program to calculate the maximum distance between any city and its nearest station ; Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxDistance ( int numOfCities , int station [ ] , int n ) { bool hasStation [ numOfCities + 1 ] = { false } ; for ( int city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } int dist = 0 ; int maxDist = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { maxDist = min ( station [ i ] , maxDist ) ; } for ( int city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return max ( maxDist , dist ) ; } int main ( ) { int numOfCities = 6 ; int station [ ] = { 3 , 1 } ; int n = sizeof ( station ) / sizeof ( station [ 0 ] ) ; cout << " Max ▁ Distance : " << findMaxDistance ( numOfCities , station , n ) ; }
Split the number into N parts such that difference between the smallest and the largest part is minimum | CPP implementation of the approach ; Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; ; void split ( int x , int n ) { if ( x < n ) cout << " - 1" << " ▁ " ; else if ( x % n == 0 ) { for ( int i = 0 ; i < n ; i ++ ) cout << ( x / n ) << " ▁ " ; } else { int zp = n - ( x % n ) ; int pp = x / n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i >= zp ) cout << ( pp + 1 ) << " ▁ " ; else cout << pp << " ▁ " ; } } } int main ( ) { int x = 5 ; int n = 3 ; split ( x , n ) ; }
Minimum time to reach a point with + t and | C ++ implementation of the above approach ; returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; long cal_minimum_time ( long X ) { long t = 0 ; long sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } int main ( ) { long n = 6 ; long ans = cal_minimum_time ( n ) ; cout << " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " << ans ; return 0 ; }
Check if a string can be rearranged to form special palindrome | C ++ implementation of the above approach ; Driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { string s = " ABCdcba " ; int u [ 26 ] = { 0 } ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isupper ( s [ i ] ) ) { u [ s [ i ] - 65 ] += 1 ; } else { u [ s [ i ] - 97 ] -= 1 ; } } bool f1 = true ; int po = 0 ; int ne = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) cout << ( " YES " ) << endl ; else if ( po == 1 && ne == 0 ) cout << ( " YES " ) << endl ; else if ( po == 0 && ne == -1 ) cout << ( " YES " ) << endl ; else cout << ( " NO " ) << endl ; }
Modify the string such that it contains all vowels at least once | C ++ 14 implementation of the above approach ; All vowels ; List to store distinct vowels ; if length of string is less than 5 then always Impossible ; Storing the distinct vowels in the string by checking if it in the list of string and not in the list of distinct vowels ; Storing the vowels which are not present in the string ; No replacement needed condition ; Replacing the characters to get all Vowels ; copy th rest of the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addAllVowel ( string str ) { char x [ ] = { ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; vector < char > y ; int length = str . length ( ) ; if ( length < 5 ) cout << " Impossible " << endl ; else { for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , x + 5 , str [ i ] ) != x + 5 and find ( y . begin ( ) , y . end ( ) , str [ i ] ) == y . end ( ) ) y . push_back ( str [ i ] ) ; } vector < char > z ; for ( int i = 0 ; i < 5 ; i ++ ) if ( find ( y . begin ( ) , y . end ( ) , x [ i ] ) == y . end ( ) ) z . push_back ( x [ i ] ) ; if ( z . empty ( ) ) cout << str << endl ; else { int cc = 0 ; vector < char > y ; for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , x + 5 , str [ i ] ) != x + 5 and find ( y . begin ( ) , y . end ( ) , str [ i ] ) == y . end ( ) ) y . push_back ( str [ i ] ) ; else { str [ i ] = z [ cc ] ; cc ++ ; } if ( cc == z . size ( ) ) break ; } cout << str << endl ; } } } int main ( int argc , char const * argv [ ] ) { string str = " ABCDEFGHI " ; addAllVowel ( str ) ; return 0 ; }
Minimum number of days required to complete the work | C ++ program to find the minimum number days required ; Function to find the minimum number days required ; initialising ans to least value possible ; sort by first i . e D ( i ) ; Calculate the minimum possible days ; return the answer ; Driver Code ; Number of works ; D1 [ i ] ; D2 [ i ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define inf  INT_MAX NEW_LINE int minimumDays ( int N , int D1 [ ] , int D2 [ ] ) { int ans = - inf ; vector < pair < int , int > > vect ; for ( int i = 0 ; i < N ; i ++ ) vect . push_back ( make_pair ( D1 [ i ] , D2 [ i ] ) ) ; sort ( vect . begin ( ) , vect . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( vect [ i ] . second >= ans ) ans = vect [ i ] . second ; else ans = vect [ i ] . first ; } return ans ; } int main ( ) { int N = 3 ; int D1 [ ] = { 6 , 5 , 4 } ; int D2 [ ] = { 1 , 2 , 3 } ; cout << minimumDays ( N , D1 , D2 ) ; return 0 ; }
Count substrings made up of a single distinct character | C ++ program for the above approach ; Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ; Driver code
#include <iostream> NEW_LINE using namespace std ; void countSubstrings ( string s ) { int ans = 0 ; int subs = 1 ; char pre = '0' ; for ( auto & i : s ) { if ( pre == i ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = i ; } cout << ans << endl ; } int main ( ) { string s = " geeksforgeeks " ; countSubstrings ( s ) ; return 0 ; }
Sum of minimum difference between consecutive elements of an array | C ++ program for finding the minimum sum of difference between consecutive elements ; function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; Driver code ; array of range
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( pair < int , int > v [ ] , int n ) { int ans , ul , ll ; ll = v [ 0 ] . first ; ul = v [ 0 ] . second ; ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( v [ i ] . first <= ul && v [ i ] . first >= ll ) || ( v [ i ] . second >= ll && v [ i ] . second <= ul ) ) { if ( v [ i ] . first > ll ) { ll = v [ i ] . first ; } if ( v [ i ] . second < ul ) { ul = v [ i ] . second ; } } else if ( v [ i ] . first > ul ) { ans += abs ( ul - v [ i ] . first ) ; ul = v [ i ] . first ; ll = v [ i ] . first ; } else if ( v [ i ] . second < ll ) { ans += abs ( ll - v [ i ] . second ) ; ul = v [ i ] . second ; ll = v [ i ] . second ; } } return ans ; } int main ( ) { pair < int , int > v [ ] = { { 1 , 3 } , { 2 , 5 } , { 6 , 8 } , { 1 , 2 } , { 2 , 3 } } ; int n = sizeof ( v ) / sizeof ( v [ 0 ] ) ; cout << solve ( v , n ) << endl ; return 0 ; }
Find the Largest Cube formed by Deleting minimum Digits from a number | C ++ code to implement maximum perfect cube formed after deleting minimum digits ; Returns vector of Pre Processed perfect cubes ; convert the cube to string and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > preProcess ( long long int n ) { vector < string > preProcessedCubes ; for ( int i = 1 ; i * i * i <= n ; i ++ ) { long long int iThCube = i * i * i ; string cubeString = to_string ( iThCube ) ; preProcessedCubes . push_back ( cubeString ) ; } return preProcessedCubes ; } string findLargestCubeUtil ( string num , vector < string > preProcessedCubes ) { reverse ( preProcessedCubes . begin ( ) , preProcessedCubes . end ( ) ) ; int totalCubes = preProcessedCubes . size ( ) ; for ( int i = 0 ; i < totalCubes ; i ++ ) { string currCube = preProcessedCubes [ i ] ; int digitsInCube = currCube . length ( ) ; int index = 0 ; int digitsInNumber = num . length ( ) ; for ( int j = 0 ; j < digitsInNumber ; j ++ ) { if ( num [ j ] == currCube [ index ] ) index ++ ; if ( digitsInCube == index ) return currCube ; } } return " Not ▁ Possible " ; } void findLargestCube ( long long int n ) { vector < string > preProcessedCubes = preProcess ( n ) ; string num = to_string ( n ) ; string ans = findLargestCubeUtil ( num , preProcessedCubes ) ; cout << " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " << n << " ▁ is ▁ " << ans << endl ; } int main ( ) { long long int n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ; return 0 ; }
Water Connection Problem | C ++ program to find efficient solution for the network ; number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; Vector a , b , c are used to store the final output ; Function performing calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; main function ; set the value of the araray to zero
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n , p ; int rd [ 1100 ] ; int wt [ 1100 ] ; int cd [ 1100 ] ; vector < int > a ; vector < int > b ; vector < int > c ; int ans ; int dfs ( int w ) { if ( cd [ w ] == 0 ) return w ; if ( wt [ w ] < ans ) ans = wt [ w ] ; return dfs ( cd [ w ] ) ; } void solve ( int arr [ ] [ 3 ] ) { int i = 0 ; while ( i < p ) { int q = arr [ i ] [ 0 ] , h = arr [ i ] [ 1 ] , t = arr [ i ] [ 2 ] ; cd [ q ] = h ; wt [ q ] = t ; rd [ h ] = q ; i ++ ; } a . clear ( ) ; b . clear ( ) ; c . clear ( ) ; for ( int j = 1 ; j <= n ; ++ j ) if ( rd [ j ] == 0 && cd [ j ] ) { ans = 1000000000 ; int w = dfs ( j ) ; a . push_back ( j ) ; b . push_back ( w ) ; c . push_back ( ans ) ; } cout << a . size ( ) << endl ; for ( int j = 0 ; j < a . size ( ) ; ++ j ) cout << a [ j ] << " ▁ " << b [ j ] << " ▁ " << c [ j ] << endl ; } int main ( ) { n = 9 , p = 6 ; memset ( rd , 0 , sizeof ( rd ) ) ; memset ( cd , 0 , sizeof ( cd ) ) ; memset ( wt , 0 , sizeof ( wt ) ) ; int arr [ ] [ 3 ] = { { 7 , 4 , 98 } , { 5 , 9 , 72 } , { 4 , 6 , 10 } , { 2 , 8 , 22 } , { 9 , 7 , 17 } , { 3 , 1 , 66 } } ; solve ( arr ) ; return 0 ; }
Print a closest string that does not contain adjacent duplicates | C ++ program to print a string with no adjacent duplicates by doing minimum changes to original string ; Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string noAdjacentDup ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = ' a ' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return s ; } int main ( ) { string s = " geeksforgeeks " ; cout << noAdjacentDup ( s ) ; return 0 ; }
Array element moved by k using single moves | C ++ program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; times = 1 ; no of wins is 1 now ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver program to test the above function
#include <iostream> NEW_LINE using namespace std ; int winner ( int a [ ] , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i ) } else if ( times >= k ) return best ; } return best ; } int main ( ) { int a [ ] = { 2 , 1 , 3 , 4 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 2 ; cout << winner ( a , n , k ) ; return 0 ; }
Paper Cut into Minimum Number of Squares | C ++ program to find minimum number of squares to cut a paper . ; Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSquare ( int a , int b ) { long long result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; long long rem = a % b ; a = b ; b = rem ; } return result ; } int main ( ) { int n = 13 , m = 29 ; cout << minimumSquare ( n , m ) ; return 0 ; }
Maximize array sum after K negations | Set 2 | A PriorityQueue based C ++ program to maximize array sum after k negations . ; Function to find Maximum sum after K negations ; Create a min heap for priority queue ; Insert all elements in f array in priority_queue ; Retrieve and remove min element ; Modify the minimum element and add back to priority queue ; Calculate the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxSum ( int a [ ] , int n , int k ) { int sum = 0 ; priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < n ; i ++ ) { pq . push ( a [ i ] ) ; } while ( k -- ) { int temp = pq . top ( ) ; pq . pop ( ) ; temp = ( temp ) * -1 ; pq . push ( temp ) ; } while ( ! pq . empty ( ) ) { sum = sum + pq . top ( ) ; pq . pop ( ) ; } return sum ; } int main ( ) { int a [ ] = { -2 , 0 , 5 , -1 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << MaxSum ( a , n , k ) ; return 0 ; }
Count of non decreasing Arrays with ith element in range [ A [ i ] , B [ i ] ] | C ++ program for the above approach ; Function to count the total number of possible valid arrays ; Make a 2D DP table ; Make a 2D prefix sum table ; Initialize all values to 0 ; Base Case ; Initialize the prefix values ; Iterate over the range and update the dp table accordingly ; Add the dp values to the prefix sum ; Update the prefix sum table ; Find the result count of arrays formed ; Return the total count of arrays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalValidArrays ( int a [ ] , int b [ ] , int N ) { int dp [ N + 1 ] [ b [ N - 1 ] + 1 ] ; int pref [ N + 1 ] [ b [ N - 1 ] + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) , memset ( pref , 0 , sizeof ( pref ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i <= b [ N - 1 ] ; i ++ ) { pref [ 0 ] [ i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = a [ i - 1 ] ; j <= b [ i - 1 ] ; j ++ ) { dp [ i ] [ j ] += pref [ i - 1 ] [ j ] ; pref [ i ] [ j ] += dp [ i ] [ j ] ; } for ( int j = 0 ; j <= b [ N - 1 ] ; j ++ ) { if ( j > 0 ) { pref [ i ] [ j ] += pref [ i ] [ j - 1 ] ; } } } int ans = 0 ; for ( int i = a [ N - 1 ] ; i <= b [ N - 1 ] ; i ++ ) { ans += dp [ N ] [ i ] ; } return ans ; } int main ( ) { int A [ ] = { 1 , 1 } ; int B [ ] = { 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << totalValidArrays ( A , B , N ) ; return 0 ; }
Count of integers in range [ L , R ] having even frequency of each digit | C ++ program for the above approach ; Stores the upper limit of the range ; Stores the overlapping states ; Recursive Function to calculate the count of valid integers in the range [ 1 , s ] using memoization ; Base Case ; If current integer has even count of digits and is not repeated ; If current state is already considered ; Stores the maximum valid digit at the current index ; Stores the count of valid integers ; If the current digit is not the most significant digit , i . e , the integer is already started ; Iterate through all valid digits ; Recursive call for ith digit at the current index ; Recursive call for integers having leading zeroes in the beginning ; Iterate through all valid digits as most significant digits ; Recursive call for ith digit at the current index ; Return answer ; Function to calculate valid number in the range [ 1 , X ] ; Initialize dp array with - 1 ; Store the range in form of string ; Return Count ; Function to find the count of integers in the range [ L , R ] such that the frequency of each digit is even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string s ; int dp [ 1024 ] [ 10 ] [ 2 ] [ 2 ] ; int calcCnt ( int mask , int len , int smaller , int started ) { if ( len == s . size ( ) ) { return ( mask == 0 && started ) ; } if ( dp [ mask ] [ len ] [ smaller ] [ started ] != -1 ) return dp [ mask ] [ len ] [ smaller ] [ started ] ; int mx = 9 ; if ( ! smaller ) { mx = s [ len ] - '0' ; } int ans = 0 ; if ( started ) { for ( int i = 0 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , smaller || ( i < s [ len ] - '0' ) , 1 ) ; } } else { ans = calcCnt ( mask , len + 1 , 1 , 0 ) ; for ( int i = 1 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , smaller || ( i < s [ len ] - '0' ) , 1 ) ; } } return dp [ mask ] [ len ] [ smaller ] [ started ] = ans ; } int countInt ( int x ) { memset ( dp , -1 , sizeof ( dp ) ) ; s = to_string ( x ) ; return calcCnt ( 0 , 0 , 0 , 0 ) ; } int countIntInRange ( int L , int R ) { return countInt ( R ) - countInt ( L - 1 ) ; } int main ( ) { int L = 32 , R = 1010 ; cout << countIntInRange ( L , R ) ; return 0 ; }
Minimize cost of swapping set bits with unset bits in a given Binary string | C ++ program for the above approach ; Function to find the minimum cost required to swap every set bit with an unset bit ; Stores the indices of set and unset bits of the string S ; Traverse the string S ; Store the indices ; Initialize a dp table of size n1 * n2 ; Set unreachable states to INF ; Fill the dp Table according to the given recurrence relation ; Update the value of dp [ i ] [ j ] ; Return the minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  1000000000 NEW_LINE int minimumCost ( string s ) { int N = s . length ( ) ; vector < int > A , B ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) { A . push_back ( i ) ; } else { B . push_back ( i ) ; } } int n1 = A . size ( ) ; int n2 = B . size ( ) ; int dp [ n1 + 1 ] [ n2 + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= n1 ; i ++ ) { dp [ i ] [ 0 ] = INF ; } for ( int i = 1 ; i <= n1 ; i ++ ) { for ( int j = 1 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = min ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j - 1 ] + abs ( A [ i - 1 ] - B [ j - 1 ] ) ) ; } } return dp [ n1 ] [ n2 ] ; } int main ( ) { string S = "1010001" ; cout << minimumCost ( S ) ; return 0 ; }