codeparrot/xlcost-text-to-code huggingface dataset:
Count number of unique ways to paint a N x 3 grid | C ++ program for the above approach ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Count of ways to pain a row with same colored ends ; Count of ways to pain a row with different colored ends ; Traverse up to ( N - 1 ) th row ; For same colored ends ; For different colored ends ; Print the total number of ways ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void waysToPaint ( int n ) { int same = 6 ; int diff = 6 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long sameTmp = 3 * same + 2 * diff ; long diffTmp = 2 * same + 2 * diff ; same = sameTmp ; diff = diffTmp ; } cout << ( same + diff ) ; } int main ( ) { int N = 2 ; waysToPaint ( N ) ; }
Minimize sum of incompatibilities of K equal | C ++ program for the above approach ; Function to find the minimum incompatibility sum ; Stores the count of element ; Traverse the array ; If number i not occurs in Map ; Put the element in the Map ; Increment the count of i in the Hash Map ; If count of i in Map is greater than K then return - 1 ; Stores all total state ; Traverse over all the state ; If number of set bit is equal to N / K ; Stores the minimum value at a state ; Call the recursive function ; Recursive function to find the minimum required value ; Base Case ; Stores the minimum value of the current state ; If dp [ state ] [ index ] is already calculated ; return dp [ state ] [ index ] ; Traverse over all the bits ; If count of set bit is N / K ; Store the new state after choosing N / K elements ; Stores the minimum and maximum of current subset ; Stores if the elements have been already selected or not ; Stores if it is possible to select N / K elements ; Traverse over the array ; If not chosen already for another subset ; If already chosen for another subset or current subset ; Mark the good false ; Mark the current number visited ; Mark the current position in mask newstate ; Update the maximum and minimum ; If good is true then Update the res ; Update the current sp state ; Return the res ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int k ; int n ; int goal ; vector < vector < int > > dp ; vector < int > bits ; int minimumIncompatibility ( vector < int > A , int K ) { n = A . size ( ) ; k = K ; goal = n / k ; map < int , int > mp ; for ( int i : A ) { if ( mp . find ( i ) != mp . end ( ) ) { mp [ i ] = 0 ; } mp [ i ] ++ ; if ( mp [ i ] > k ) return -1 ; } int state = ( 1 << n ) - 1 ; for ( int i = 0 ; i <= state ; i ++ ) { if ( __builtin_popcount ( i ) == goal ) bits . push_back ( i ) ; } dp . resize ( 1 << n , vector < int > ( k , -1 ) ) ; return dfs ( A , state , 0 ) ; } int dfs ( vector < int > A , int state , int index ) { if ( index >= k ) { return 0 ; } int res = 1000 ; if ( dp [ state ] [ index ] != -1 ) { return dp [ state ] [ index ] ; } for ( int bit : bits ) { if ( __builtin_popcount ( bit ) == goal ) { int newstate = state ; int mn = 100 , mx = -1 ; vector < bool > visit ( n + 1 , false ) ; bool good = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( bit & ( 1 << j ) ) != 0 ) { if ( visit [ A [ j ] ] == true || ( state & ( 1 << j ) ) == 0 ) { good = false ; break ; } visit [ A [ j ] ] = true ; newstate = newstate ^ ( 1 << j ) ; mx = max ( mx , A [ j ] ) ; mn = min ( mn , A [ j ] ) ; } } if ( good ) { res = min ( res , mx - mn + dfs ( A , newstate , index + 1 ) ) ; } } } dp [ state ] [ index ] = res ; return res ; } int main ( ) { vector < int > arr = { 1 , 2 , 1 , 4 } ; int K = 2 ; cout << ( minimumIncompatibility ( arr , K ) ) ; }
Maximize sum by selecting X different | C ++ program for the above approach ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindMaxS ( int X , int Y , int Z , int n , vector < int > & A , vector < int > & B , vector < int > & C ) { if ( X < 0 or Y < 0 or Z < 0 ) return INT_MIN ; if ( n < 0 ) return 0 ; int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = max ( ch , max ( ca , max ( co , no ) ) ) ; return maximum ; } int main ( ) { int X = 1 ; int Y = 1 ; int Z = 1 ; vector < int > A = { 10 , 0 , 5 } ; vector < int > B = { 5 , 10 , 0 } ; vector < int > C = { 0 , 5 , 10 } ; int n = B . size ( ) ; cout << FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; }
Rearrange array by interchanging positions of even and odd elements in the given array | C ++ program for the above approach ; Function to replace each even element by odd and vice - versa in a given array ; Traverse array ; If current element is even then swap it with odd ; Perform Swap ; Change the sign ; If current element is odd then swap it with even ; Perform Swap ; Change the sign ; Marked element positive ; Print final array ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void replace ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 == 0 && arr [ j ] % 2 != 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } else if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 != 0 && arr [ j ] % 2 == 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = abs ( 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 ] ) ; replace ( arr , n ) ; }
Count half nodes in a Binary tree ( Iterative and Recursive ) | C ++ program to count half nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of half Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree shown in above example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; unsigned int gethalfCount ( struct Node * root ) { if ( root == NULL ) return 0 ; int res = 0 ; if ( ( root -> left == NULL && root -> right != NULL ) || ( root -> left != NULL && root -> right == NULL ) ) res ++ ; res += ( gethalfCount ( root -> left ) + gethalfCount ( root -> right ) ) ; return res ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( void ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; cout << gethalfCount ( root ) ; return 0 ; }
Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum | C ++ program to implement the above approach ; Function to find the minimum cost to reduce given array to a single element by replacing consecutive K array elements ; If ( N - 1 ) is not multiple of ( K - 1 ) ; Store prefix sum of the array ; Iterate over the range [ 1 , N ] ; Update prefixSum [ i ] ; dp [ i ] [ j ] : Store minimum cost to merge array elements interval [ i , j ] ; L : Stores length of interval [ i , j ] ; Iterate over each interval [ i , j ] of length L in in [ 0 , N ] ; Stores index of last element of the interval [ i , j ] ; If L is greater than K ; Update dp [ i ] [ j ] ; If ( L - 1 ) is multiple of ( K - 1 ) ; Update dp [ i ] [ j ] ; Return dp [ 0 ] [ N - 1 ] ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumCostToMergeK ( int arr [ ] , int K , int N ) { if ( ( N - 1 ) % ( K - 1 ) != 0 ) { return -1 ; } int prefixSum [ N + 1 ] = { 0 } ; for ( int i = 1 ; i < ( N + 1 ) ; i ++ ) { prefixSum [ i ] = ( prefixSum [ i - 1 ] + arr [ i - 1 ] ) ; } int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int L = K ; L < ( N + 1 ) ; L ++ ) { for ( int i = 0 ; i < ( N - L + 1 ) ; i ++ ) { int j = i + L - 1 ; if ( L > K ) { int temp = INT_MAX ; for ( int x = i ; x < j ; x += K - 1 ) { temp = min ( temp , dp [ i ] [ x ] + dp [ x + 1 ] [ j ] ) ; } dp [ i ] [ j ] = temp ; } if ( ( L - 1 ) % ( K - 1 ) == 0 ) { dp [ i ] [ j ] += ( prefixSum [ j + 1 ] - prefixSum [ i ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } int main ( ) { int arr [ ] = { 3 , 5 , 1 , 2 , 6 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumCostToMergeK ( arr , K , N ) ; }
Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | C ++ program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  8 NEW_LINE int check ( int memo [ ] [ N ] , int i , int j , int * A ) { if ( i == N - 1 ) return 1 ; if ( memo [ i ] [ j ] != -1 ) return memo [ i ] [ j ] ; int flag = 0 , k ; for ( k = i + 1 ; k < N ; k ++ ) { if ( A [ k ] - A [ i ] > j + 1 ) break ; if ( A [ k ] - A [ i ] >= j - 1 && A [ k ] - A [ i ] <= j + 1 ) flag = check ( memo , k , A [ k ] - A [ i ] , A ) ; if ( flag ) break ; } memo [ i ] [ j ] = flag ; return memo [ i ] [ j ] ; } void checkEndReach ( int A [ ] , int K ) { int memo [ N ] [ N ] ; memset ( memo , -1 , sizeof ( memo ) ) ; int startIndex = 1 ; if ( check ( memo , startIndex , K , A ) ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int A [ ] = { 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 } ; int K = 1 ; checkEndReach ( A , K ) ; return 0 ; }
Maximum subsequence sum such that no K elements are consecutive | C ++ program for the above approach ; Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Initialise dp state ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Max_Sum ( int arr [ ] , int K , int N ) { int dp [ N + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; int prefix [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } dp [ 0 ] = 0 ; for ( int i = 1 ; i < K ; i ++ ) { dp [ i ] = prefix [ i ] ; } for ( int i = K ; i <= N ; ++ i ) { for ( int j = i ; j >= ( i - K + 1 ) ; j -- ) { dp [ i ] = max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) ; } } return dp [ N ] ; } int main ( ) { int arr [ ] = { 4 , 12 , 22 , 18 , 34 , 12 , 25 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int K = 5 ; cout << Max_Sum ( arr , K , N ) ; return 0 ; }
Count possible splits of sum N into K integers such that the minimum is at least P | C ++ program for the above approach ; Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N into K integers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Driver Code ; Given K , N , and P
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int waysToSplitN ( int k , int n , int P ) { int new_N = n - k * P ; return binomialCoeff ( new_N + k - 1 , new_N ) ; } int main ( ) { int K = 3 , N = 8 , P = 2 ; cout << waysToSplitN ( K , N , P ) ; return 0 ; }
Minimum pair merge operations required to make Array non | C ++ program to implement the above approach ; Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( vector < int > & a ) { int n = a . size ( ) ; vector < int > dp ( n + 1 , 0 ) , val ( n + 1 , 0 ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { long long sum = a [ i ] ; int j = i ; while ( j + 1 < n and sum < val [ j + 1 ] ) { j ++ ; sum += a [ j ] ; } dp [ i ] = ( j - i ) + dp [ j + 1 ] ; val [ i ] = sum ; } return dp [ 0 ] ; } int main ( ) { vector < int > arr = { 1 , 5 , 3 , 9 , 1 } ; cout << solve ( arr ) ; }
Count full nodes in a Binary tree ( Iterative and Recursive ) | C ++ program to count full nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root ; Initialize count of full nodes ; Enqueue left child ; Enqueue right child ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; unsigned int getfullCount ( struct Node * node ) { if ( ! node ) return 0 ; queue < Node * > q ; q . push ( node ) ; int count = 0 ; while ( ! q . empty ( ) ) { struct Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left && temp -> right ) count ++ ; if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; } return count ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( void ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; cout << getfullCount ( root ) ; return 0 ; }
Count of Distinct Substrings occurring consecutively in a given String | C ++ Program to implement the above approach ; Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Stores the distinct substring ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctSimilarSubstrings ( string str ) { int n = str . size ( ) ; if ( n <= 1 ) { return 0 ; } vector < vector < int > > dp ( n + 1 , vector < int > ( n + 1 , 0 ) ) ; unordered_set < string > substrings ; for ( int j = n - 1 ; j >= 0 ; j -- ) { for ( int i = j - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == str [ j ] ) { dp [ i ] [ j ] = dp [ i + 1 ] [ j + 1 ] + 1 ; } else { dp [ i ] [ j ] = 0 ; } if ( dp [ i ] [ j ] >= j - i ) { substrings . insert ( str . substr ( i , j - i ) ) ; } } } return substrings . size ( ) ; } int main ( ) { string str = " geeksgeeksforgeeks " ; cout << distinctSimilarSubstrings ( str ) ; return 0 ; }
Finding shortest path between any two nodes using Floyd Warshall Algorithm | C ++ program to find the shortest path between any two nodes using Floyd Warshall Algorithm . ; Infinite value for array ; Initializing the distance and Next array ; No edge between node i and j ; Function construct the shortest path between u and v ; If there 's no path between  node u and v, simply return  an empty array ; Storing the path in a vector ; Standard Floyd Warshall Algorithm with little modification Now if we find that dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] then we modify next [ i ] [ j ] = next [ i ] [ k ] ; We cannot travel through edge that doesn 't exist ; Print the shortest path ; Driver code ; Function to initialise the distance and Next array ; Calling Floyd Warshall Algorithm , this will update the shortest distance as well as Next array ; Path from node 1 to 3 ; Path from node 0 to 2 ; path from node 3 to 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  100 NEW_LINE const int INF = 1e7 ; int dis [ MAXN ] [ MAXN ] ; int Next [ MAXN ] [ MAXN ] ; void initialise ( int V , vector < vector < int > > & graph ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { dis [ i ] [ j ] = graph [ i ] [ j ] ; if ( graph [ i ] [ j ] == INF ) Next [ i ] [ j ] = -1 ; else Next [ i ] [ j ] = j ; } } } vector < int > constructPath ( int u , int v ) { if ( Next [ u ] [ v ] == -1 ) return { } ; vector < int > path = { u } ; while ( u != v ) { u = Next [ u ] [ v ] ; path . push_back ( u ) ; } return path ; } void floydWarshall ( int V ) { for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( dis [ i ] [ k ] == INF dis [ k ] [ j ] == INF ) continue ; if ( dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] ) { dis [ i ] [ j ] = dis [ i ] [ k ] + dis [ k ] [ j ] ; Next [ i ] [ j ] = Next [ i ] [ k ] ; } } } } } void printPath ( vector < int > & path ) { int n = path . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) cout << path [ i ] << " ▁ - > ▁ " ; cout << path [ n - 1 ] << endl ; } int main ( ) { int V = 4 ; vector < vector < int > > graph = { { 0 , 3 , INF , 7 } , { 8 , 0 , 2 , INF } , { 5 , INF , 0 , 1 } , { 2 , INF , INF , 0 } } ; initialise ( V , graph ) ; floydWarshall ( V ) ; vector < int > path ; cout << " Shortest ▁ path ▁ from ▁ 1 ▁ to ▁ 3 : ▁ " ; path = constructPath ( 1 , 3 ) ; printPath ( path ) ; cout << " Shortest ▁ path ▁ from ▁ 0 ▁ to ▁ 2 : ▁ " ; path = constructPath ( 0 , 2 ) ; printPath ( path ) ; cout << " Shortest ▁ path ▁ from ▁ 3 ▁ to ▁ 2 : ▁ " ; path = constructPath ( 3 , 2 ) ; printPath ( path ) ; return 0 ; }
Count sequences of length K having each term divisible by its preceding term | C ++ Program to implement the above approach ; Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto sqrt ( N ) ; Function to return the count of sequences of length K having all terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 ] [ i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 ] [ i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of all dp [ i - 1 ] [ vp [ j ] [ k ] ] ; vp [ j ] [ k ] stores all factors of j ; Store the sum in A [ i ] [ j ] ; Sum of all dp [ K ] [ j ] obtain all K length sequences ending with j ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE vector < ll int > vp [ 2009 ] ; void finding_factors ( ll int n ) { ll int i , a ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i == n ) { vp [ n ] . push_back ( i ) ; } else { vp [ n ] . push_back ( i ) ; vp [ n ] . push_back ( n / i ) ; } } } } ll int countSeq ( ll int N , ll int K ) { ll int i , j , k ; ll int dp [ 109 ] [ 109 ] = { 0 } ; for ( i = 1 ; i <= N ; i ++ ) { finding_factors ( i ) ; dp [ 0 ] [ i ] = 0 ; dp [ 1 ] [ i ] = 1 ; } for ( i = 2 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { ll int sum = 0 ; for ( k = 0 ; k < vp [ j ] . size ( ) ; k ++ ) { sum = ( sum + dp [ i - 1 ] [ vp [ j ] [ k ] ] ) ; } dp [ i ] [ j ] = sum ; } } ll int ans = 0 ; for ( j = 1 ; j <= N ; j ++ ) { ans = ( ans + dp [ K ] [ j ] ) ; } return ans ; } int main ( ) { ll int N , K ; N = 3 ; K = 2 ; cout << countSeq ( N , K ) << endl ; return 0 ; }
Largest possible square submatrix with maximum AND value | C ++ program to find the length of longest possible square submatrix with maximum AND value from the given matrix ; Function to calculate and return the length of square submatrix with maximum AND value ; Extract dimensions ; Auxiliary array ; c : Stores the maximum value in the matrix p : Stores the number of elements in the submatrix having maximum AND value ; Iterate over the matrix to fill the auxiliary matrix ; Find the max element in the matrix side by side ; Fill first row and column with 1 's ; For every cell , check if the elements at the left , top and top left cells from the current cell are equal or not ; Store the minimum possible submatrix size these elements are part of ; Store 1 otherwise ; checking maximum value ; If the maximum AND value occurs more than once ; Update the maximum size of submatrix ; final output ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MAX_value ( vector < vector < int > > arr ) { int row = arr . size ( ) ; int col = arr [ 0 ] . size ( ) ; int dp [ row ] [ col ] ; memset ( dp , sizeof ( dp ) , 0 ) ; int i = 0 , j = 0 ; int c = arr [ 0 ] [ 0 ] , p = 0 ; int d = row ; for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( c < arr [ i ] [ j ] ) { c = arr [ i ] [ j ] ; } if ( i == 0 j == 0 ) { dp [ i ] [ j ] = 1 ; } else { if ( arr [ i - 1 ] [ j - 1 ] == arr [ i ] [ j ] && arr [ i - 1 ] [ j ] == arr [ i ] [ j ] && arr [ i ] [ j - 1 ] == arr [ i ] [ j ] ) { dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j - 1 ] , min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ) + 1 ; } else { dp [ i ] [ j ] = 1 ; } } } } for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( arr [ i ] [ j ] == c ) { if ( p < dp [ i ] [ j ] ) { p = dp [ i ] [ j ] ; } } } } return p * p ; } int main ( ) { vector < vector < int > > arr = { { 9 , 9 , 3 , 3 , 4 , 4 } , { 9 , 9 , 7 , 7 , 7 , 4 } , { 1 , 2 , 7 , 7 , 7 , 4 } , { 4 , 4 , 7 , 7 , 7 , 4 } , { 5 , 5 , 1 , 1 , 2 , 7 } , { 2 , 7 , 1 , 1 , 4 , 4 } } ; cout << MAX_value ( arr ) << endl ; return 0 ; }