codeparrot/xlcost-text-to-code huggingface dataset:
Maximum product of two non | C ++ program to find maximum product of two non - intersecting paths ; Returns maximum length path in subtree rooted at u after removing edge connecting u and v ; To find lengths of first and second maximum in subtrees . currMax is to store overall maximum . ; loop through all neighbors of u ; if neighbor is v , then skip it ; call recursively with current neighbor as root ; get max from one side and update ; store total length by adding max and second max ; update current max by adding 1 , i . e . current node is included ; method returns maximum product of length of two non - intersecting paths ; one by one removing all edges and calling dfs on both subtrees ; calling dfs on subtree rooted at g [ i ] [ j ] , excluding edge from g [ i ] [ j ] to i . ; calling dfs on subtree rooted at i , edge from i to g [ i ] [ j ] ; Utility function to add an undirected edge ( u , v ) ; Driver code to test above methods ; there are N edges , so + 1 for nodes and + 1 for 1 - based indexing
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dfs ( vector < int > g [ ] , int & curMax , int u , int v ) { int max1 = 0 , max2 = 0 , total = 0 ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { if ( g [ u ] [ i ] == v ) continue ; total = max ( total , dfs ( g , curMax , g [ u ] [ i ] , u ) ) ; if ( curMax > max1 ) { max2 = max1 ; max1 = curMax ; } else max2 = max ( max2 , curMax ) ; } total = max ( total , max1 + max2 ) ; curMax = max1 + 1 ; return total ; } int maxProductOfTwoPaths ( vector < int > g [ ] , int N ) { int res = INT_MIN ; int path1 , path2 ; for ( int i = 1 ; i < N + 2 ; i ++ ) { for ( int j = 0 ; j < g [ i ] . size ( ) ; j ++ ) { int curMax = 0 ; path1 = dfs ( g , curMax , g [ i ] [ j ] , i ) ; curMax = 0 ; path2 = dfs ( g , curMax , i , g [ i ] [ j ] ) ; res = max ( res , path1 * path2 ) ; } } return res ; } void addEdge ( vector < int > g [ ] , int u , int v ) { g [ u ] . push_back ( v ) ; g [ v ] . push_back ( u ) ; } int main ( ) { int edges [ ] [ 2 ] = { { 1 , 8 } , { 2 , 6 } , { 3 , 1 } , { 5 , 3 } , { 7 , 8 } , { 8 , 4 } , { 8 , 6 } } ; int N = sizeof ( edges ) / sizeof ( edges [ 0 ] ) ; vector < int > g [ N + 2 ] ; for ( int i = 0 ; i < N ; i ++ ) addEdge ( g , edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; cout << maxProductOfTwoPaths ( g , N ) << endl ; return 0 ; }
Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | C ++ implementation to find minimum number of steps to reach N from 1 ; Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minsteps ( int n ) { int ans = 0 ; while ( n > 1 ) { if ( n % 5 == 0 ) { ans ++ ; n = n / 5 ; continue ; } else if ( n % 4 == 0 ) { ans ++ ; n = n / 4 ; continue ; } else if ( n % 3 == 0 ) { ans ++ ; n = n / 3 ; continue ; } else if ( n % 2 == 0 ) { ans ++ ; n = n / 2 ; continue ; } return -1 ; } return ans ; } int main ( ) { int n = 10 ; cout << Minsteps ( n ) ; return 0 ; }
Find the maximum difference after applying the given operations two times on a number | C ++ program to find the maximum difference after two given operations on a number ; Function that returns the maximum difference after two given operations on a number ; Initialize the strings to make operations ; Store length of the string ; Make the maximum number using the first operation ; Find the first digit which is not equal to '9' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '9' ; Break after the operation is completed ; Check if first digit is equal to '1' ; Find the first digit which is greater than '1' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '0' ; Break after the operation is completed ; Select the first digit for the given operation ; Replace all the selected ' digit ' with '1' ; Return the difference after converting into integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDifference ( int num ) { string maximum = to_string ( num ) ; string minimum = to_string ( num ) ; int n = maximum . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( maximum [ i ] != '9' ) { char digit = maximum [ i ] ; for ( int j = i ; j < n ; j ++ ) { if ( maximum [ j ] == digit ) maximum [ j ] = '9' ; } break ; } } if ( minimum [ 0 ] == '1' ) { for ( int i = 1 ; i < n ; i ++ ) { if ( minimum [ i ] - '0' > 1 ) { char digit = minimum [ i ] ; for ( int j = i ; j < n ; j ++ ) { if ( digit == minimum [ j ] ) minimum [ j ] = '0' ; } break ; } } } else { char digit = minimum [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( minimum [ i ] == digit ) minimum [ i ] = '1' ; } } return ( stoi ( maximum ) - stoi ( minimum ) ) ; } int main ( ) { int N = 1101157 ; cout << minDifference ( N ) ; return 0 ; }
Check if the square of a number is divisible by K or not | C ++ implementation to check if the square of X is divisible by K ; Function to return if square of X is divisible by K ; Finding gcd of x and k ; Dividing k by their gcd ; Check for divisibility of X by reduced K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkDivisible ( int x , int k ) { int g = __gcd ( x , k ) ; k /= g ; if ( x % k == 0 ) { cout << " YES STRNEWLINE " ; } else { cout << " NO STRNEWLINE " ; } } int main ( ) { int x = 6 , k = 9 ; checkDivisible ( x , k ) ; return 0 ; }
Find the minimum value of the given expression over all pairs of the array | C ++ program to find the minimum value of the given expression over all pairs of the array ; Function to find the minimum value of the expression ; The expression simplifies to finding the minimum xor value pair ; Calculate min xor of consecutive pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumValue ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int minXor = INT_MAX ; int val = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { val = arr [ i ] ^ arr [ i + 1 ] ; minXor = min ( minXor , val ) ; } return minXor ; } int main ( ) { int N = 6 ; int A [ N ] = { 12 , 3 , 14 , 5 , 9 , 8 } ; cout << MinimumValue ( A , N ) ; return 0 ; }
Find length of longest substring with at most K normal characters | C ++ implementation to Find length of longest substring with at most K normal characters ; Function to find maximum length of normal substrings ; keeps count of normal characters ; indexes of substring ; maintain length of longest substring with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with substring length ; get position of character ; check if character is normal then decrement count ; Driver code ; initialise the string
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxNormalSubstring ( string & P , string & Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { int pos = P [ right ] - ' a ' ; if ( Q [ pos ] == '0' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = max ( ans , right - left ) ; } while ( left < right ) { int pos = P [ left ] - ' a ' ; left ++ ; if ( Q [ pos ] == '0' ) count -- ; if ( count < K ) break ; } } return ans ; } int main ( ) { string P = " giraffe " , Q = "01111001111111111011111111" ; int K = 2 ; int N = P . length ( ) ; cout << maxNormalSubstring ( P , Q , K , N ) ; return 0 ; }
Make all the elements of array even with given operations | C ++ program to make all array element even ; Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int countOperations ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] & 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) return -1 ; } return count ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << countOperations ( arr , n ) ; return 0 ; }
Count of pairs with difference at most K with no element repeating | C ++ implementation to count the number of pairs whose difference is atmost K in an array ; Function to count the number of pairs whose difference is atmost K in an array ; Sorting the Array ; Variable to store the count of pairs whose difference is atmost K ; Loop to consider the consecutive pairs of the array ; if Pair found increment the index by 2 ; Driver Code ; Function Call
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int k , int n ) { sort ( arr , arr + n ) ; int pair = 0 ; int index = 0 ; while ( index < n - 1 ) { if ( arr [ index + 1 ] - arr [ index ] <= k ) { pair += 1 ; index += 2 ; } else { index += 1 ; } } return pair ; } int main ( ) { int arr [ ] = { 1 , 4 , 3 , 7 , 5 } ; int k = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int count = countPairs ( arr , k , n ) ; cout << count << endl ; ; }
Maximum profit by selling N items at two markets | C ++ implementation of the approach ; Function to calculate max profit ; Prefix sum array for profitA [ ] ; Suffix sum array for profitB [ ] ; If all the items are sold in market A ; Find the maximum profit when the first i items are sold in market A and the rest of the items are sold in market B for all possible values of i ; If all the items are sold in market B ; Driver code ; Function to calculate max profit
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int profitA [ ] , int profitB [ ] , int n ) { int preSum [ n ] ; preSum [ 0 ] = profitA [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; } int suffSum [ n ] ; suffSum [ n - 1 ] = profitB [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; } int res = preSum [ n - 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { res = max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; } res = max ( res , suffSum [ 0 ] ) ; return res ; } int main ( ) { int profitA [ ] = { 2 , 3 , 2 } ; int profitB [ ] = { 10 , 30 , 40 } ; int n = sizeof ( profitA ) / sizeof ( int ) ; cout << maxProfit ( profitA , profitB , n ) ; return 0 ; }
Minimum number of edges that need to be added to form a triangle | C ++ implementation of the approach ; Function to return the minimum number of edges that need to be added to the given graph such that it contains at least one triangle ; adj is the adjacency matrix such that adj [ i ] [ j ] = 1 when there is an edge between i and j ; As the graph is undirected so there will be an edge between ( i , j ) and ( j , i ) ; To store the required count of edges ; For every possible vertex triplet ; If the vertices form a triangle ; If no edges are present ; If only 1 edge is required ; Two edges are required ; Driver code ; Number of nodes ; Storing the edges in a vector of pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minEdges ( vector < pair < int , int > > v , int n ) { vector < vector < int > > adj ; adj . resize ( n + 1 ) ; for ( int i = 0 ; i < adj . size ( ) ; i ++ ) adj [ i ] . resize ( n + 1 , 0 ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { adj [ v [ i ] . first ] [ v [ i ] . second ] = 1 ; adj [ v [ i ] . second ] [ v [ i ] . first ] = 1 ; } int edgesNeeded = 3 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { for ( int k = j + 1 ; k <= n ; k ++ ) { if ( adj [ i ] [ j ] && adj [ j ] [ k ] && adj [ k ] [ i ] ) return 0 ; if ( ! ( adj [ i ] [ j ] adj [ j ] [ k ] adj [ k ] [ i ] ) ) edgesNeeded = min ( edgesNeeded , 3 ) ; else { if ( ( adj [ i ] [ j ] && adj [ j ] [ k ] ) || ( adj [ j ] [ k ] && adj [ k ] [ i ] ) || ( adj [ k ] [ i ] && adj [ i ] [ j ] ) ) { edgesNeeded = 1 ; } else edgesNeeded = min ( edgesNeeded , 2 ) ; } } } } return edgesNeeded ; } int main ( ) { int n = 3 ; vector < pair < int , int > > v = { { 1 , 2 } , { 1 , 3 } } ; cout << minEdges ( v , n ) ; return 0 ; }
Length of the longest subsequence such that xor of adjacent elements is non | C ++ implementation of the approach ; Function to find the length of the longest subsequence such that the XOR of adjacent elements in the subsequence must be non - decreasing ; Computing xor of all the pairs of elements and store them along with the pair ( i , j ) ; Sort all possible xor values ; Initialize the dp array ; Calculating the dp array for each possible position and calculating the max length that ends at a particular index ; Taking maximum of all position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestXorSubsequence ( int arr [ ] , int n ) { vector < pair < int , pair < int , int > > > v ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { v . push_back ( make_pair ( arr [ i ] ^ arr [ j ] , make_pair ( i , j ) ) ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( auto i : v ) { dp [ i . second . second ] = max ( dp [ i . second . second ] , 1 + dp [ i . second . first ] ) ; } int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) ans = max ( ans , dp [ i ] ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 12 , 6 , 7 , 13 , 14 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LongestXorSubsequence ( arr , n ) ; return 0 ; }
Delete Edge to minimize subtree sum difference | C ++ program to minimize subtree sum difference by one edge deletion ; DFS method to traverse through edges , calculating subtree sum at each node and updating the difference between subtrees ; loop for all neighbors except parent and aggregate sum over all subtrees ; store sum in current node 's subtree index ; at one side subtree sum is ' sum ' and other side subtree sum is ' totalSum ▁ - ▁ sum ' so their difference will be totalSum - 2 * sum , by which we 'll update  res  ; Method returns minimum subtree sum difference ; Calculating total sum of tree and initializing subtree sum 's by vertex values ; filling edge data structure ; calling DFS method at node 0 , with parent as - 1 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int u , int parent , int totalSum , vector < int > edge [ ] , int subtree [ ] , int & res ) { int sum = subtree [ u ] ; for ( int i = 0 ; i < edge [ u ] . size ( ) ; i ++ ) { int v = edge [ u ] [ i ] ; if ( v != parent ) { dfs ( v , u , totalSum , edge , subtree , res ) ; sum += subtree [ v ] ; } } subtree [ u ] = sum ; if ( u != 0 && abs ( totalSum - 2 * sum ) < res ) res = abs ( totalSum - 2 * sum ) ; } int getMinSubtreeSumDifference ( int vertex [ ] , int edges [ ] [ 2 ] , int N ) { int totalSum = 0 ; int subtree [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { subtree [ i ] = vertex [ i ] ; totalSum += vertex [ i ] ; } vector < int > edge [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) { edge [ edges [ i ] [ 0 ] ] . push_back ( edges [ i ] [ 1 ] ) ; edge [ edges [ i ] [ 1 ] ] . push_back ( edges [ i ] [ 0 ] ) ; } int res = INT_MAX ; dfs ( 0 , -1 , totalSum , edge , subtree , res ) ; return res ; } int main ( ) { int vertex [ ] = { 4 , 2 , 1 , 6 , 3 , 5 , 2 } ; int edges [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 2 , 4 } , { 2 , 5 } , { 3 , 6 } } ; int N = sizeof ( vertex ) / sizeof ( vertex [ 0 ] ) ; cout << getMinSubtreeSumDifference ( vertex , edges , N ) ; return 0 ; }
Minimum operations required to make every element greater than or equal to K | C ++ implementation of above approach ; C ++ function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindMinOperation ( int a [ ] , int n , int k ) { priority_queue < int , vector < int > , greater < int > > Q ; for ( int i = 0 ; i < n ; i ++ ) Q . push ( a [ i ] ) ; int ans = 0 ; while ( 1 ) { if ( Q . top ( ) >= k ) break ; if ( Q . size ( ) < 2 ) return -1 ; int x = Q . top ( ) ; Q . pop ( ) ; int y = Q . top ( ) ; Q . pop ( ) ; int z = ( x * y ) / __gcd ( x , y ) ; Q . push ( z ) ; ans ++ ; } return ans ; } int main ( ) { int a [ ] = { 3 , 5 , 7 , 6 , 8 } ; int k = 8 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << FindMinOperation ( a , n , k ) ; }
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | C ++ implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver code
using System ; using System . Collections ; class GfG { #include <bits/stdc++.h> NEW_LINE using namespace std ; string Check ( int a [ ] [ 2 ] , int b [ ] [ 2 ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] > b [ i ] [ j ] ) swap ( a [ i ] [ j ] , b [ i ] [ j ] ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m - 1 ; j ++ ) if ( a [ i ] [ j ] >= a [ i ] [ j + 1 ] b [ i ] [ j ] >= b [ i ] [ j + 1 ] ) return " No " ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] >= a [ i + 1 ] [ j ] b [ i ] [ j ] >= b [ i + 1 ] [ j ] ) return " No " ; return " Yes " ; } int main ( ) { int n = 2 , m = 2 ; int a [ ] [ 2 ] = { { 2 , 10 } , { 11 , 5 } } ; int b [ ] [ 2 ] = { { 9 , 4 } , { 3 , 12 } } ; cout << ( Check ( a , b , n , m ) ) ; }
Find the lexicographically smallest string which satisfies the given condition | C ++ implementation of the approach ; Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
#include <iostream> NEW_LINE using namespace std ; string smallestString ( int N , int A [ ] ) { char ch = ' a ' ; string S = " " ; if ( N < 1 A [ 0 ] != 1 ) { S = " - 1" ; return S ; } S += ch ; ch ++ ; for ( int i = 1 ; i < N ; i ++ ) { int diff = A [ i ] - A [ i - 1 ] ; if ( diff > 1 diff < 0 A [ i ] > 26 ) { S = " - 1" ; return S ; } else if ( diff == 0 ) S += ' a ' ; else { S += ch ; ch ++ ; } } return S ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << smallestString ( n , arr ) ; return 0 ; }
Count of alphabets whose ASCII values can be formed with the digits of N | C ++ implementation of the approach ; Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't contain  current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBePicked ( int digits [ ] , int num ) { int copyDigits [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) copyDigits [ i ] = digits [ i ] ; while ( num > 0 ) { int digit = num % 10 ; if ( copyDigits [ digit ] == 0 ) return false ; else copyDigits [ digit ] -= 1 ; num = floor ( num / 10 ) ; } return true ; } int countAlphabets ( long n ) { int count = 0 ; int digits [ 10 ] = { 0 } ; while ( n > 0 ) { int digit = n % 10 ; digits [ digit ] += 1 ; n = floor ( n / 10 ) ; } for ( int i = 97 ; i <= 122 ; i ++ ) if ( canBePicked ( digits , i ) ) count += 1 ; for ( int i = 65 ; i < 91 ; i ++ ) if ( canBePicked ( digits , i ) ) count += 1 ; return count ; } int main ( ) { long n = 1623455078 ; cout << ( countAlphabets ( n ) ) ; }
Largest number less than X having at most K set bits | C ++ implementation of the approach ; Function to return the greatest number <= X having at most K set bits . ; Remove rightmost set bits one by one until we count becomes k ; Return the required number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int greatestKBits ( int X , int K ) { int set_bit_count = __builtin_popcount ( X ) ; if ( set_bit_count <= K ) return X ; int diff = set_bit_count - K ; for ( int i = 0 ; i < diff ; i ++ ) X &= ( X - 1 ) ; return X ; } int main ( ) { int X = 21 , K = 2 ; cout << greatestKBits ( X , K ) ; return 0 ; }