codeparrot/xlcost-text-to-code huggingface dataset:
Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | C ++ program for the above approach ; Function to return all prime numbers smaller than N ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all its entries as true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Driver Code ; Given arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100005 NEW_LINE vector < int > SieveOfEratosthenes ( ) { bool seive [ MAX ] ; memset ( seive , true , sizeof ( seive ) ) ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( seive [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) { seive [ i ] = false ; } } } vector < int > v ; for ( int p = 2 ; p < MAX ; p ++ ) { if ( seive [ p ] ) { v . push_back ( p ) ; } } return v ; } void build ( int dp [ ] , int arr [ ] , int N ) { dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; vector < int > prime = SieveOfEratosthenes ( ) ; int pref [ N + 1 ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; for ( int j = 0 ; j <= prime . size ( ) ; j ++ ) { int r = i - 1 ; int l = r - prime [ j ] + 1 ; if ( l < 0 ) break ; int temp = 0 ; temp = pref [ r + 1 ] - pref [ l ] ; if ( l - 2 >= 0 ) temp += dp [ l - 2 + 1 ] ; dp [ i ] = max ( dp [ i ] , temp ) ; } } } void maxSumSubseq ( int arr [ ] , int N ) { int dp [ N + 1 ] ; build ( dp , arr , N ) ; cout << dp [ N ] ; } int main ( ) { int arr [ ] = { 10 , 10 , 7 , 10 , 10 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSumSubseq ( arr , N ) ; return 0 ; }
Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions | C ++ program for the above approach ; Function to find the maximum score for one possible tree having N nodes N - 1 Edges ; Number of nodes ; Initialize dp [ ] [ ] ; Score with 0 vertices is 0 ; Traverse the nodes from 1 to N ; Find maximum scores for each sum ; Iterate over degree of new node ; Update the current state ; Return maximum score for N node tree having 2 ( N - 1 ) sum of degree ; Driver Code ; Given array of scores ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxScore ( vector < int > & arr ) { int N = arr . size ( ) ; N ++ ; vector < vector < int > > dp ( N + 1 , vector < int > ( 2 * N , -100000 ) ) ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int s = 1 ; s <= 2 * ( N - 1 ) ; s ++ ) { for ( int j = 1 ; j <= N - 1 and j <= s ; j ++ ) { dp [ i ] [ s ] = max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] ) ; } } } return dp [ N ] [ 2 * ( N - 1 ) ] ; } int main ( ) { vector < int > arr = { 1 , 3 , 0 } ; cout << maxScore ( arr ) ; return 0 ; }
Minimize cost of choosing and skipping array elements to reach end of the given array | C ++ program for the above approach ; Function to find the minimum cost to reach the end of the array from the first element ; Store the results ; Consider first index cost ; Find answer for each position i ; First Element ; Second Element ; For remaining element ; Consider min cost for skipping ; Last index represents the minimum total cost ; Driver Code ; Given X ; Given array cost [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumCost ( int * cost , int n , int x ) { vector < int > dp ( n + 2 , 0 ) ; dp [ 0 ] = cost [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i == 1 ) dp [ i ] = cost [ i ] + dp [ i - 1 ] ; if ( i == 2 ) dp [ i ] = cost [ i ] + min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) ; if ( i >= 3 ) dp [ i ] = cost [ i ] + min ( dp [ i - 1 ] , min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ; } cout << dp [ n - 1 ] ; } int main ( ) { int X = 4 ; int cost [ ] = { 6 , 3 , 9 , 2 , 1 , 3 } ; int N = sizeof ( cost ) / sizeof ( cost [ 0 ] ) ; minimumCost ( cost , N , X ) ; return 0 ; }
Maximum possible sum of non | C ++ program for the above approach ; Function to find the maximum sum not exceeding K possible by selecting a subset of non - adjacent elements ; Base Case ; Not selecting current element ; If selecting current element is possible ; Return answer ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int a [ ] , int n , int k ) { if ( n <= 0 ) return 0 ; int option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } int main ( ) { int arr [ ] = { 50 , 10 , 20 , 30 , 40 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 100 ; cout << ( maxSum ( arr , N , K ) ) ; }
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | C ++ program to implement the above approach ; Function to check if a string satisfy the given condition or not ; Stores the length of string ; Stores the previous character of the string ; Stores the count of consecutive equal characters ; Traverse the string ; If current character is equal to the previous character ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Reset value of cnt ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Function to count all distinct binary strings that satisfy the given condition ; Stores the length of str ; If length of str is N ; If str satisfy the given condition ; If str does not satisfy the given condition ; Append a character '0' at end of str ; Append a character '1' at end of str ; Return total count of binary strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkStr ( string str , int P , int Q ) { int N = str . size ( ) ; char prev = str [ 0 ] ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == prev ) { cnt ++ ; } else { if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str [ i ] ; } if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } return true ; } int cntBinStr ( string str , int N , int P , int Q ) { int len = str . size ( ) ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } int X = cntBinStr ( str + '0' , N , P , Q ) ; int Y = cntBinStr ( str + '1' , N , P , Q ) ; return X + Y ; } int main ( ) { int N = 5 , P = 2 , Q = 3 ; cout << cntBinStr ( " " , N , P , Q ) ; return 0 ; }
Count possible permutations of given array satisfying the given conditions | C ++ Program to implement the above approach ; Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binCoff ( int N , int R ) { int res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( int i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } int cntPermutation ( int N ) { int cntPerm ; int C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntPermutation ( N / 2 ) ; return 0 ; }
Smallest submatrix with Kth maximum XOR | C ++ program for above approach ; Function to print smallest index of Kth maximum xors value of submatrices ; Dimensions of matrix ; Stores xors values for every index ; Min heap to find the kth maximum xors value ; Stores indices for corresponding xors values ; Traversing matrix to calculate xors values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xors [ i ] [ j ] ; Stores the kth maximum element ; Print the required index ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestPosition ( int m [ ] [ 3 ] , int k , int row ) { int n = row ; int mm = row ; int xors [ n ] [ mm ] ; priority_queue < int , vector < int > , greater < int > > minHeap ; map < int , vector < int > > map ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xors [ i - 1 ] [ j ] : 0 ; int b = j - 1 >= 0 ? xors [ i ] [ j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xors [ i - 1 ] [ j - 1 ] : 0 ; xors [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c ; minHeap . push ( xors [ i ] [ j ] ) ; if ( minHeap . size ( ) > k ) { minHeap . pop ( ) ; } if ( map . find ( xors [ i ] [ j ] ) == map . end ( ) ) map [ xors [ i ] [ j ] ] = { i , j } ; } } int kth_max_e = minHeap . top ( ) ; cout << ( map [ kth_max_e ] [ 0 ] + 1 ) << " ▁ " << ( map [ kth_max_e ] [ 1 ] + 1 ) ; } int main ( ) { int m [ ] [ 3 ] = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k , 3 ) ; }
Length of Longest Palindrome Substring | C ++ program for the above approach ; Function to obtain the length of the longest palindromic substring ; Length of given string ; Stores the maximum length ; Iterate over the string ; Iterate over the string ; Check for palindrome ; If string [ i , j - i + 1 ] is palindromic ; Return length of LPS ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestPalSubstr ( string str ) { int n = str . size ( ) ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i ; j < str . length ( ) ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str [ i + k ] != str [ j - k ] ) flag = 0 ; if ( flag && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } return maxLength ; } int main ( ) { string str = " forgeeksskeegfor " ; cout << longestPalSubstr ( str ) ; return 0 ; }
Maximize cost to empty an array by removing contiguous subarrays of equal elements | C ++ program for the above approach ; Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function Call ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 101 ] [ 101 ] [ 101 ] ; int helper ( int arr [ ] , int left , int right , int count , int m ) { if ( left > right ) return 0 ; if ( dp [ left ] [ right ] [ count ] != -1 ) { return dp [ left ] [ right ] [ count ] ; } int ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( int i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left ] [ right ] [ count ] = ans ; return ans ; } int maxPoints ( int arr [ ] , int n , int m ) { int len = n ; memset ( dp , -1 , sizeof ( dp ) ) ; return helper ( arr , 0 , len - 1 , 0 , m ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 } ; int M = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxPoints ( arr , N , M ) ; return 0 ; }
Minimum cost to convert M to N by repeated addition of its even divisors | C ++ program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver code ; Function call ; If conversion is not possible ; Print the cost
#include <iostream> NEW_LINE using namespace std ; int inf = 1000000008 ; int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } int main ( ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; if ( minimum_cost == inf ) minimum_cost = -1 ; cout << minimum_cost ; return 0 ; }
Represent a number as the sum of positive numbers ending with 9 | C ++ program for the above approach ; Function to find the minimum count of numbers ending with 9 to form N ; Extract last digit of N ; Calculate the last digit ; If the last digit satisfies the condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCountOfNumbers ( int N ) { int k = N % 10 ; int z = N - ( 9 * ( 9 - k ) ) ; if ( z >= 9 && z % 10 == 9 ) { return 10 - k ; } else return -1 ; } int main ( ) { int N = 156 ; cout << minCountOfNumbers ( N ) ; return 0 ; }
Binary Matrix after flipping submatrices in given range for Q queries | C ++ program to implement the above approach ; Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void manipulation ( vector < vector < int > > & matrix , vector < int > & q ) { int x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( int i = x1 - 1 ; i < x2 ; i ++ ) { for ( int j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i ] [ j ] == 1 ) matrix [ i ] [ j ] = 0 ; else matrix [ i ] [ j ] = 1 ; } } } void queries_fxn ( vector < vector < int > > & matrix , vector < vector < int > > & queries ) { for ( auto q : queries ) manipulation ( matrix , q ) ; } int main ( ) { vector < vector < int > > matrix = { { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; vector < vector < int > > queries = { { 1 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 3 } } ; queries_fxn ( matrix , queries ) ; cout << " [ " ; for ( int i = 0 ; i < matrix . size ( ) ; i ++ ) { cout << " [ " ; for ( int j = 0 ; j < matrix [ i ] . size ( ) ; j ++ ) cout << matrix [ i ] [ j ] << " ▁ " ; if ( i == matrix . size ( ) - 1 ) cout << " ] " ; else cout << " ] , ▁ " ; } cout << " ] " ; }
Minimum repeated addition of even divisors of N required to convert N to M | C ++ program for the above approach ; INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Driver Code ; Given N and M ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int INF = 1e7 ; int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return op ; } int min_operations ( int N , int M ) { int op = min_op ( N , M ) ; if ( op >= INF ) cout << " - 1" ; else cout << op << " STRNEWLINE " ; } int main ( ) { int N = 6 , M = 24 ; min_operations ( N , M ) ; return 0 ; }
Minimum length of subsequence having unit GCD | C ++ program for the above approach ; Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Driver code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findPrimeFactors ( int n ) { vector < int > primeFactors ( 9 , 0 ) ; int j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; vector < int > PrimeFactors ( j ) ; for ( int i = 0 ; i < j ; i ++ ) { PrimeFactors [ i ] = primeFactors [ i ] ; } return PrimeFactors ; } void findShortestSubsequence ( vector < int > & dp , vector < int > a , int index , vector < int > primeFactors ) { int n = a . size ( ) ; for ( int j = index ; j < n ; j ++ ) { int bitmask = 0 ; for ( int p = 0 ; p < primeFactors . size ( ) ; p ++ ) { if ( ( a [ j ] % primeFactors [ p ] ) == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( int i = 0 ; i < dp . size ( ) ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } void printMinimumLength ( vector < int > a ) { int Min = a . size ( ) + 1 ; for ( int i = 0 ; i < a . size ( ) - 1 ; i ++ ) { vector < int > primeFactors = findPrimeFactors ( a [ i ] ) ; int n = primeFactors . size ( ) ; vector < int > dp ( 1 << n , a . size ( ) + 1 ) ; int setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; Min = min ( dp [ 0 ] , Min ) ; } if ( Min == ( a . size ( ) + 1 ) ) cout << -1 << endl ; else cout << Min << endl ; } int main ( ) { vector < int > arr = { 2 , 6 , 12 , 3 } ; printMinimumLength ( arr ) ; return 0 ; }
Maximum sum of Bitwise XOR of all elements of two equal length subsets | C ++ program for the above approach ; Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array arr [ ] ; Declare Initialize the dp states ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorSum ( int a [ ] , int n , int mask , int dp [ ] ) { if ( dp [ mask ] != -1 ) { return dp [ mask ] ; } int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } int main ( ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; int dp [ ( 1 << n ) + 5 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << ( xorSum ( arr , n , 0 , dp ) ) ; }
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition | C ++ program for the above approach ; Function to generate the fibonacci number ; First two number of fibonacci sqequence ; Function to find maximum ways to represent num as the sum of fibonacci number ; Generate the Canonical form of given number ; Reverse the number ; Base condition of dp1 and dp2 ; Iterate from 1 to cnt ; Calculate dp1 [ ] ; Calculate dp2 [ ] ; Return final ans ; Driver Code ; Function call to generate the fibonacci numbers ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long fib [ 101 ] , dp1 [ 101 ] ; long long dp2 [ 101 ] , v [ 101 ] ; void fibonacci ( ) { fib [ 1 ] = 1 ; fib [ 2 ] = 2 ; for ( int i = 3 ; i <= 87 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } int find ( int num ) { int cnt = 0 ; for ( int i = 87 ; i > 0 ; i -- ) { if ( num >= fib [ i ] ) { v [ cnt ++ ] = i ; num -= fib [ i ] ; } } reverse ( v , v + cnt ) ; dp1 [ 0 ] = 1 ; dp2 [ 0 ] = ( v [ 0 ] - 1 ) / 2 ; for ( int i = 1 ; i < cnt ; i ++ ) { dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ] ; dp2 [ i ] = ( ( v [ i ] - v [ i - 1 ] ) / 2 ) * dp2 [ i - 1 ] + ( ( v [ i ] - v [ i - 1 ] - 1 ) / 2 ) * dp1 [ i - 1 ] ; } return ( dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ] ) ; } int main ( ) { fibonacci ( ) ; int num = 13 ; cout << find ( num ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to find count of N - digit numbers with single digit XOR ; Range of numbers ; Calculate XOR of digits ; If XOR <= 9 , then increment count ; Print the count ; Driver Code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNums ( int N ) { int l = ( int ) pow ( 10 , N - 1 ) ; int r = ( int ) pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int xorr = 0 , temp = i ; while ( temp > 0 ) { xorr = xorr ^ ( temp % 10 ) ; temp /= 10 ; } if ( xorr <= 9 ) count ++ ; } cout << count ; } int main ( ) { int N = 2 ; countNums ( N ) ; }
Maximize length of Non | C ++ program to implement the above approach ; Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i ] [ j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void main_fun ( int arr [ ] , int n ) { int dp [ 4 ] [ n ] ; memset ( dp , 0 , sizeof ( dp [ 0 ] [ 0 ] * 4 * n ) ) ; if ( arr [ 0 ] == 0 ) dp [ 0 ] [ 0 ] = 1 ; else dp [ 1 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; else dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) dp [ 1 ] [ i ] = max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ; else dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { dp [ 2 ] [ i ] = max ( dp [ 2 ] [ i - 1 ] + 1 , max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ; } else dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { dp [ 3 ] [ i ] = max ( dp [ 3 ] [ i - 1 ] + 1 , max ( dp [ 2 ] [ i - 1 ] + 1 , max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ) ; } else dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ] ; } int ans = max ( dp [ 2 ] [ n - 1 ] , max ( dp [ 1 ] [ n - 1 ] , max ( dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ) ) ) ; cout << ( ans ) ; } int main ( ) { int n = 4 ; int arr [ ] = { 0 , 1 , 0 , 1 } ; main_fun ( arr , n ) ; return 0 ; }