codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | C ++ approach for the above approach ; Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int ans = 0 ; string arr = to_string ( n ) ; for ( int i = 0 ; i < arr . length ( ) ; i ++ ) { ans += int ( arr [ i ] ) ; } return ans ; } void getMax ( int arr [ ] , int n ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int digitSum = getSum ( arr [ i ] ) ; if ( digitSum % 3 == 0 ) maxRem0 = max ( maxRem0 , arr [ i ] ) ; else if ( digitSum % 3 == 1 ) rem1 += arr [ i ] ; else rem2 += arr [ i ] ; } cout << ( maxRem0 + max ( rem1 , rem2 ) ) ; } int main ( ) { int arr [ ] = { 23 , 12 , 43 , 3 , 56 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getMax ( arr , n ) ; }
Minimum edge reversals to make a root | C ++ program to find min edge reversal to make every node reachable from root ; method to dfs in tree and populates disRev values ; visit current node ; looping over all neighbors ; distance of v will be one more than distance of u ; initialize back edge count same as parent node 's count ; if there is a reverse edge from u to i , then only update ; return total reversal in subtree rooted at u ; method prints root and minimum number of edge reversal ; number of nodes are one more than number of edges ; data structure to store directed tree ; disRev stores two values - distance and back edge count from root node ; add 0 weight in direction of u to v ; add 1 weight in reverse direction ; initialize all variables ; dfs populates disRev data structure and store total reverse edge counts ; for ( int i = 0 ; i < V ; i ++ ) { cout << i << " ▁ : ▁ " << disRev [ i ] . first << " ▁ " << disRev [ i ] . second << endl ; } ; loop over all nodes to choose minimum edge reversal ; ( reversal in path to i ) + ( reversal in all other tree parts ) ; choose minimum among all values ; print the designated root and total edge reversal made ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dfs ( vector < pair < int , int > > g [ ] , pair < int , int > disRev [ ] , bool visit [ ] , int u ) { visit [ u ] = true ; int totalRev = 0 ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] [ i ] . first ; if ( ! visit [ v ] ) { disRev [ v ] . first = disRev [ u ] . first + 1 ; disRev [ v ] . second = disRev [ u ] . second ; if ( g [ u ] [ i ] . second ) { disRev [ v ] . second = disRev [ u ] . second + 1 ; totalRev ++ ; } totalRev += dfs ( g , disRev , visit , v ) ; } } return totalRev ; } void printMinEdgeReverseForRootNode ( int edges [ ] [ 2 ] , int e ) { int V = e + 1 ; vector < pair < int , int > > g [ V ] ; pair < int , int > disRev [ V ] ; bool visit [ V ] ; int u , v ; for ( int i = 0 ; i < e ; i ++ ) { u = edges [ i ] [ 0 ] ; v = edges [ i ] [ 1 ] ; g [ u ] . push_back ( make_pair ( v , 0 ) ) ; g [ v ] . push_back ( make_pair ( u , 1 ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { visit [ i ] = false ; disRev [ i ] . first = disRev [ i ] . second = 0 ; } int root = 0 ; int totalRev = dfs ( g , disRev , visit , root ) ; int res = INT_MAX ; for ( int i = 0 ; i < V ; i ++ ) { int edgesToRev = ( totalRev - disRev [ i ] . second ) + ( disRev [ i ] . first - disRev [ i ] . second ) ; if ( edgesToRev < res ) { res = edgesToRev ; root = i ; } } cout << root << " ▁ " << res << endl ; } int main ( ) { int edges [ ] [ 2 ] = { { 0 , 1 } , { 2 , 1 } , { 3 , 2 } , { 3 , 4 } , { 5 , 4 } , { 5 , 6 } , { 7 , 6 } } ; int e = sizeof ( edges ) / sizeof ( edges [ 0 ] ) ; printMinEdgeReverseForRootNode ( edges , e ) ; return 0 ; }
Check if 2 * K + 1 non | C ++ program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkString ( string s , int k ) { int n = s . size ( ) ; if ( 2 * k + 1 > n ) { cout << " No " ; return ; } string a = s . substr ( 0 , k ) ; string b = s . substr ( n - k , k ) ; reverse ( b . begin ( ) , b . end ( ) ) ; if ( a == b ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; return 0 ; }
Minimum number of socks required to picked to have at least K pairs of the same color | C ++ program for the above approach ; Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findMin ( int arr [ ] , int N , int k ) { int pairs = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return -1 ; else return 2 * k + N - 1 ; } int main ( ) { int arr [ 3 ] = { 4 , 5 , 6 } ; int K = 3 ; cout << findMin ( arr , 3 , K ) ; return 0 ; }
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | C ++ program for the above approach ; Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumFlips ( int A , int B , int C ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int x = 0 , y = 0 , z = 0 ; if ( A & ( 1 << i ) ) { x = 1 ; } if ( B & ( 1 << i ) ) { y = 1 ; } if ( C & ( 1 << i ) ) { z = 1 ; } if ( z == 0 ) { if ( x ) { res ++ ; } if ( y ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } int main ( ) { int A = 2 , B = 6 , C = 5 ; cout << minimumFlips ( A , B , C ) ; return 0 ; }
Queries to calculate Bitwise AND of an array with updates | C ++ program for the above approach ; Store the number of set bits at each position ; Function to precompute the prefix count array ; Iterate over the range [ 0 , 31 ] ; Set the bit at position i if arr [ 0 ] is set at position i ; Traverse the array and take prefix sum ; Update prefixCount [ i ] [ j ] ; Function to find the Bitwise AND of all array elements ; Stores the required result ; Iterate over the range [ 0 , 31 ] ; Stores the number of set bits at position i ; If temp is N , then set ith position in the result ; Print the result ; Function to update the prefix count array in each query ; Iterate through all the bits of the current number ; Store the bit at position i in the current value and the new value ; If bit2 is set and bit1 is unset , then increase the set bits at position i by 1 ; If bit1 is set and bit2 is unset , then decrease the set bits at position i by 1 ; Function to find the bitwise AND of the array after each query ; Fill the prefix count array ; Traverse the queries ; Store the index and the new value ; Store the current element at the index ; Update the array element ; Apply the changes to the prefix count array ; Print the bitwise AND of the modified array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prefixCount [ 32 ] [ 10000 ] ; void findPrefixCount ( vector < int > arr , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { prefixCount [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < size ; j ++ ) { prefixCount [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefixCount [ i ] [ j ] += prefixCount [ i ] [ j - 1 ] ; } } } void arrayBitwiseAND ( int size ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int temp = prefixCount [ i ] [ size - 1 ] ; if ( temp == size ) result = ( result | ( 1 << i ) ) ; } cout << result << " ▁ " ; } void applyQuery ( int currentVal , int newVal , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { int bit1 = ( ( currentVal >> i ) & 1 ) ; int bit2 = ( ( newVal >> i ) & 1 ) ; if ( bit2 > 0 && bit1 == 0 ) prefixCount [ i ] [ size - 1 ] ++ ; else if ( bit1 > 0 && bit2 == 0 ) prefixCount [ i ] [ size - 1 ] -- ; } } void findbitwiseAND ( vector < vector < int > > queries , vector < int > arr , int N , int M ) { findPrefixCount ( arr , N ) ; for ( int i = 0 ; i < M ; i ++ ) { int id = queries [ i ] [ 0 ] ; int newVal = queries [ i ] [ 1 ] ; int currentVal = arr [ id ] ; arr [ id ] = newVal ; applyQuery ( currentVal , newVal , N ) ; arrayBitwiseAND ( N ) ; } } int main ( ) { vector < int > arr { 1 , 2 , 3 , 4 , 5 } ; vector < vector < int > > queries { { 0 , 2 } , { 3 , 3 } , { 4 , 2 } } ; int N = arr . size ( ) ; int M = queries . size ( ) ; findbitwiseAND ( queries , arr , N , M ) ; return 0 ; }
Find the winner of a game of donating i candies in every i | C ++ Program for the above approach ; Function to find the winning player in a game of donating i candies to opponent in i - th move ; Steps in which number of candies of player A finishes ; Steps in which number of candies of player B finishes ; If A 's candies finishes first ; Otherwise ; Driver Code ; Candies possessed by player A ; Candies possessed by player B
#include <bits/stdc++.h> NEW_LINE using namespace std ; int stepscount ( int a , int b ) { int chanceA = 2 * a - 1 ; int chanceB = 2 * b ; if ( chanceA < chanceB ) { cout << " B " ; } else if ( chanceB < chanceA ) { cout << " A " ; } return 0 ; } int main ( ) { int A = 2 ; int B = 3 ; stepscount ( A , B ) ; return 0 ; }
Check if a point is inside , outside or on a Hyperbola | C ++ program for the above approach ; Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( pow ( ( x - h ) , 2 ) / pow ( a , 2 ) ) - ( pow ( ( y - k ) , 2 ) / pow ( b , 2 ) ) ; if ( p > 1 ) { cout << " Outside " ; } else if ( p == 1 ) { cout << " On ▁ the ▁ Hyperbola " ; } else { cout << " Inside " ; } } int main ( ) { int h = 0 , k = 0 , x = 2 ; int y = 1 , a = 4 , b = 5 ; checkpoint ( h , k , x , y , a , b ) ; return 0 ; }
Count subarrays having even Bitwise XOR | C ++ program for the above approach ; Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Driver Code ; Given array ; Stores the size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenXorSubarray ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int XOR = 0 ; for ( int j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; evenXorSubarray ( arr , N ) ; return 0 ; }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | C ++ program for the above approach ; Function to find the Kth element in the required permutation ; Store the required result ; If K is in the first N / 2 elements , print K * 2 ; Otherwise , K is greater than N / 2 ; If N is even ; If N is odd ; Print the required result ; Driver Code ; functions call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findKthElement ( int N , int K ) { int ans = 0 ; if ( K <= N / 2 ) { ans = K * 2 ; } else { if ( N % 2 == 0 ) { ans = ( K * 2 ) - N - 1 ; } else { ans = ( K * 2 ) - N ; } } cout << ans ; } int main ( ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; return 0 ; }
Minimize replacements with values up to K to make sum of two given arrays equal | C ++ program for the above approach ; Function to find minimum number of replacements required to make the sum of two arrays equal ; Stores the sum of a [ ] ; Stores the sum of b [ ] ; Calculate sum of the array a [ ] ; Calculate sum of the array b [ ] ; Stores the difference between a [ ] and b [ ] ; Left and Right pointer to traverse the array a [ ] ; Left and Right pointer to traverse the array b [ ] ; Stores the count of moves ; Sort the arrays in ascending order ; Iterate while diff != 0 and l1 <= r1 or l2 <= r2 ; If diff is greater than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; If diff is less than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; Increment count of res by one ; If diff is 0 , then return res ; Otherwise , return - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makeSumEqual ( vector < int > a , vector < int > b , int K , int M , int N ) { int sum1 = 0 ; int sum2 = 0 ; for ( int el : a ) sum1 += el ; for ( int el : b ) sum2 += el ; int diff = sum1 - sum2 ; int l1 = 0 , r1 = M - 1 ; int l2 = 0 , r2 = N - 1 ; int res = 0 ; sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; while ( l1 <= r1 l2 <= r2 ) { if ( diff == 0 ) { break ; } if ( diff > 0 ) { if ( l2 <= r2 && l1 <= r1 ) { if ( K - b [ l2 ] < a [ r1 ] - 1 ) { int sub = min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else if ( l1 <= r1 ) { int sub = min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else { if ( l1 <= r1 && l2 <= r2 ) { if ( K - a [ l1 ] < b [ r2 ] - 1 ) { int sub = min ( b [ r2 ] - 1 , -1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = min ( K - a [ l1 ] , -1 * diff ) ; diff += sub ; a [ l1 ] -= sub ; l1 ++ ; } } else if ( l2 <= r2 ) { int sub = min ( b [ r2 ] - 1 , -1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = min ( K - a [ l1 ] , diff ) ; diff += sub ; a [ l1 ] += sub ; l1 ++ ; } } res ++ ; } if ( diff == 0 ) return res ; else return -1 ; } int main ( ) { vector < int > A = { 1 , 4 , 3 } ; vector < int > B = { 6 , 6 , 6 } ; int M = A . size ( ) ; int N = B . size ( ) ; int K = 6 ; cout << makeSumEqual ( A , B , K , M , N ) ; return 0 ; }
Modify a Binary String by flipping characters such that any pair of indices consisting of 1 s are neither co | Function to modify a string such that there doesn 't exist any pair of indices consisting of 1s, whose GCD is 1 and are divisible by each other ; Flips characters at indices 4 N , 4 N - 2 , 4 N - 4 . ... upto N terms ; Print the string ; Driver code ; Initialize the string S ; function call
#include <iostream> NEW_LINE using namespace std ; void findString ( char S [ ] , int N ) { int strLen = 4 * N ; for ( int i = 1 ; i <= N ; i ++ ) { S [ strLen - 1 ] = '1' ; strLen -= 2 ; } for ( int i = 0 ; i < 4 * N ; i ++ ) { cout << S [ i ] ; } } int main ( ) { int N = 2 ; char S [ 4 * N ] ; for ( int i = 0 ; i < 4 * N ; i ++ ) S [ i ] = '0' ; findString ( S , N ) ; return 0 ; }
Check if a string can be split into two substrings with equal number of vowels | C ++ program for the above approach ; Function to check if any character is a vowel or not ; Lowercase vowels ; Uppercase vowels ; Otherwise ; Function to check if string S can be split into two substrings with equal number of vowels ; Stores the count of vowels in the string S ; Traverse over the string ; If S [ i ] is vowel ; Stores the count of vowels upto the current index ; Traverse over the string ; If S [ i ] is vowel ; If vowelsTillNow and totalVowels are equal ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; if ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) return true ; return false ; } string containsEqualStrings ( string S ) { int totalVowels = 0 ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { if ( isVowel ( S [ i ] ) ) totalVowels ++ ; } int vowelsTillNow = 0 ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { if ( isVowel ( S [ i ] ) ) { vowelsTillNow ++ ; totalVowels -- ; if ( vowelsTillNow == totalVowels ) { return " Yes " ; } } } return " No " ; } int main ( ) { string S = " geeks " ; cout << ( containsEqualStrings ( S ) ) ; }
Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | C ++ program to implement the above approach ; Function to calculate maximum sum possible of a matrix by multiplying pairs of adjacent elements with - 1 any number of times ( possibly zero ) ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Driver Code ; Given matrix ; Dimensions of matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getMaxSum ( vector < vector < int > > A , int M , int N ) { int sum = 0 ; int negative = 0 ; int minVal = INT_MAX ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum += abs ( A [ i ] [ j ] ) ; if ( A [ i ] [ j ] < 0 ) { negative ++ ; } minVal = min ( minVal , abs ( A [ i ] [ j ] ) ) ; } } if ( negative % 2 ) { sum -= 2 * minVal ; } cout << sum ; } int main ( ) { vector < vector < int > > A = { { 4 , -8 , 6 } , { 3 , 7 , 2 } } ; int M = A . size ( ) ; int N = A [ 0 ] . size ( ) ; getMaxSum ( A , M , N ) ; return 0 ; }
Calculate total wall area of houses painted | C ++ program for the above approach ; Function to find the total area of walls painted in N row - houses ; Stores total area of N row - houses that needs to be painted ; Traverse the array of wall heights ; Update total area painted ; Update total ; Traverse all the houses and print the shared walls ; Update total ; Print total area needs to paint ; Driver Code ; Given N , W & L ; Given heights of houses ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void areaToPaint ( int N , int W , int L , int Heights [ ] ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += 2 * Heights [ i ] * W ; } total += L * ( Heights [ 0 ] + Heights [ N - 1 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { total += L * abs ( Heights [ i ] - Heights [ i - 1 ] ) ; } cout << total ; } int main ( ) { int N = 7 , W = 1 , L = 1 ; int Heights [ N ] = { 4 , 3 , 1 , 2 , 3 , 4 , 2 } ; areaToPaint ( N , W , L , Heights ) ; }
Generate a matrix with each row and column of given sum | C ++ program for the above approach ; Function to generate a matrix with sum of each row equal to sum of r [ ] and sum of each column equal to sum of c [ ] ; Initialize a matrix ; Traverse each cell ( i , j ) of the matrix ; Assign the minimum of the row or column value ; Subtract the minimum from both row and column sum ; Print the matrix obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > restoreGem ( vector < int > & r , vector < int > & c ) { vector < vector < int > > dp ( r . size ( ) , vector < int > ( c . size ( ) , 0 ) ) ; for ( int i = 0 ; i < r . size ( ) ; i ++ ) { for ( int j = 0 ; j < c . size ( ) ; j ++ ) { int m = min ( r [ i ] , c [ j ] ) ; dp [ i ] [ j ] = m ; r [ i ] -= m ; c [ j ] -= m ; } } return dp ; } void printMatrix ( vector < vector < int > > ans , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cout << ans [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { vector < int > rowSum = { 5 , 7 , 10 } ; vector < int > colSum = { 8 , 6 , 8 } ; vector < vector < int > > ans = restoreGem ( rowSum , colSum ) ; printMatrix ( ans , rowSum . size ( ) , colSum . size ( ) ) ; }
Maximize sum of count of distinct prime factors of K array elements | C ++ program for the above approach ; Function to find the maximum sum of count of distinct prime factors of K array elements ; Stores the count of distinct primes ; Stores 1 and 0 at prime and non - prime indices respectively ; Initialize the count of factors to 0 ; Sieve of Eratosthenes ; Count of factors of a prime number is 1 ; Increment CountDistinct of all multiples of i ; Mark its multiples non - prime ; Stores the maximum sum of distinct prime factors of K array elements ; Stores the count of all distinct prime factors ; Traverse the array to find count of all array elements ; Maximum sum of K prime factors of array elements ; Check for the largest prime factor ; Increment sum ; Decrement its count and K ; Print the maximum sum ; Driver code ; Given array ; Size of the array ; Given value of K
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE int maxSumOfDistinctPrimeFactors ( int arr [ ] , int N , int K ) { int CountDistinct [ MAX + 1 ] ; bool prime [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( long long int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( long long int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int sum = 0 ; int PrimeFactor [ 20 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { PrimeFactor [ CountDistinct [ arr [ i ] ] ] ++ ; } for ( int i = 19 ; i >= 1 ; i -- ) { while ( PrimeFactor [ i ] > 0 ) { sum += i ; PrimeFactor [ i ] -- ; K -- ; if ( K == 0 ) break ; } if ( K == 0 ) break ; } cout << sum ; } int main ( ) { int arr [ ] = { 6 , 9 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; maxSumOfDistinctPrimeFactors ( arr , N , K ) ; return 0 ; }
Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | C ++ Program to implement the above approach ; Function to count the changes required such that no palindromic substring of length exceeding 1 is present in the string ; Base Case ; Stores the count ; Iterate over the string ; Palindromic Substring of Length 2 ; Replace the next character ; Increment changes ; Palindromic Substring of Length 3 ; Replace the next character ; Increment changes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxChange ( string str ) { if ( str . size ( ) <= 1 ) { return 0 ; } int minChanges = 0 ; for ( int i = 0 ; i < str . size ( ) - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } } return minChanges ; } int main ( ) { string str = " bbbbbbb " ; cout << maxChange ( str ) ; return 0 ; }