codeparrot/xlcost-text-to-code huggingface dataset:
Smallest power of 2 greater than or equal to n | C ++ program to find smallest power of 2 greater than or equal to n ; First n in the below condition is for the case where n is 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int nextPowerOf2 ( unsigned int n ) { unsigned count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } int main ( ) { unsigned int n = 0 ; cout << nextPowerOf2 ( n ) ; return 0 ; }
Add elements in start to sort the array | Variation of Stalin Sort | C ++ implementation to sort the array by using the variation of the Stalin sort ; Function to sort the array ; Iterator < Integer > index = arr . iterator ( ) ; ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void variationStalinsort ( vector < int > arr ) { int j = 0 ; while ( true ) { int moved = 0 ; for ( int i = 0 ; i < ( arr . size ( ) - 1 - j ) ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { vector < int > :: iterator index ; int temp ; index = arr . begin ( ) + i + 1 ; temp = arr [ i + 1 ] ; arr . erase ( index ) ; arr . insert ( arr . begin ( ) + moved , temp ) ; moved ++ ; } } j ++ ; if ( moved == 0 ) { break ; } } for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { cout << arr [ i ] << " , ▁ " ; } } int main ( ) { vector < int > arr = { 2 , 1 , 4 , 3 , 6 , 5 , 8 , 7 , 10 , 9 } ; variationStalinsort ( arr ) ; }
Sort an Array which contain 1 to N values in O ( N ) using Cycle Sort | C ++ program for the above approach ; Function to swap two a & b value ; Function to print array element ; Traverse the array ; Function to sort the array in O ( N ) ; Traverse the array ; If the current element is at correct position ; Else swap the current element with it 's correct position ; Driver Code ; Function call to sort the array ; Function call to print the array
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void swap ( int * a , int * b ) { int temp = * a ; * a = * b ; * b = temp ; } void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << ' ▁ ' ; } } void sortArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; ) { if ( arr [ i ] == i + 1 ) { i ++ ; } else { swap ( & arr [ i ] , & arr [ arr [ i ] - 1 ] ) ; } } } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArray ( arr , N ) ; printArray ( arr , N ) ; return 0 ; }
Maximum sum of values of N items in 0 | C ++ Program to implement the above approach ; Function to find the maximum value ; base condition ; K elements already reduced to half of their weight ; Dont include item ; If weight of the item is less than or equal to the remaining weight then include the item ; Return the maximum of both cases ; If the weight reduction to half is possible ; Skip the item ; Include item with full weight if weight of the item is less than the remaining weight ; Include item with half weight if half weight of the item is less than the remaining weight ; Return the maximum of all 3 cases ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum ( int value [ ] , int weight [ ] , int weight1 , int flag , int K , int index , int val_len ) { if ( index >= val_len ) { return 0 ; } if ( flag == K ) { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 , val_len ) ; int full = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 , val_len ) ; } return max ( full , skip ) ; } else { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 , val_len ) ; int full = 0 ; int half = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 , val_len ) ; } if ( weight [ index ] / 2 <= weight1 ) { half = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] / 2 , flag , K , index + 1 , val_len ) ; } return max ( full , max ( skip , half ) ) ; } } int main ( ) { int value [ ] = { 17 , 20 , 10 , 15 } ; int weight [ ] = { 4 , 2 , 7 , 5 } ; int K = 1 ; int W = 4 ; int val_len = sizeof ( value ) / sizeof ( value [ 0 ] ) ; cout << ( maximum ( value , weight , W , 0 , K , 0 , val_len ) ) ; return 0 ; }
d | C ++ program to find the size of the minimum dominating set of the tree ; Definition of a tree node ; Helper function that allocates a new node ; DP array to precompute and store the results ; minDominatingSettion to return the size of the minimum dominating set of the array ; Base case ; Setting the compulsory value if needed ; Check if the answer is already computed ; If it is compulsory to select the node ; Choose the node and set its children as covered ; If it is covered ; If the current node is neither covered nor needs to be selected compulsorily ; Store the result ; Driver code ; initialising the DP array ; Constructing the tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1005 NEW_LINE struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ( ) ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } int dp [ N ] [ 5 ] [ 5 ] ; int minDominatingSet ( Node * root , int covered , int compulsory ) { if ( ! root ) return 0 ; if ( ! root -> left and ! root -> right and ! covered ) compulsory = true ; if ( dp [ root -> data ] [ covered ] [ compulsory ] != -1 ) return dp [ root -> data ] [ covered ] [ compulsory ] ; if ( compulsory ) { return dp [ root -> data ] [ covered ] [ compulsory ] = 1 + minDominatingSet ( root -> left , 1 , 0 ) + minDominatingSet ( root -> right , 1 , 0 ) ; } if ( covered ) { return dp [ root -> data ] [ covered ] [ compulsory ] = min ( 1 + minDominatingSet ( root -> left , 1 , 0 ) + minDominatingSet ( root -> right , 1 , 0 ) , minDominatingSet ( root -> left , 0 , 0 ) + minDominatingSet ( root -> right , 0 , 0 ) ) ; } int ans = 1 + minDominatingSet ( root -> left , 1 , 0 ) + minDominatingSet ( root -> right , 1 , 0 ) ; if ( root -> left ) { ans = min ( ans , minDominatingSet ( root -> left , 0 , 1 ) + minDominatingSet ( root -> right , 0 , 0 ) ) ; } if ( root -> right ) { ans = min ( ans , minDominatingSet ( root -> left , 0 , 0 ) + minDominatingSet ( root -> right , 0 , 1 ) ) ; } return dp [ root -> data ] [ covered ] [ compulsory ] = ans ; } signed main ( ) { memset ( dp , -1 , sizeof ( dp ) ) ; Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 4 ) ; root -> left -> left -> left = newNode ( 5 ) ; root -> left -> left -> left -> left = newNode ( 6 ) ; root -> left -> left -> left -> right = newNode ( 7 ) ; root -> left -> left -> left -> right -> right = newNode ( 10 ) ; root -> left -> left -> left -> left -> left = newNode ( 8 ) ; root -> left -> left -> left -> left -> right = newNode ( 9 ) ; cout << minDominatingSet ( root , 0 , 0 ) << endl ; return 0 ; }
Number of subsets with zero sum |  ; variable to store states of dp ; To find the number of subsets with sum equal to 0 Since S can be negative , we will maxSum to it to make it positive ; Base cases ; Returns the value if a state is already solved ; If the state is not visited , then continue ; Recurrence relation ; Returning the value ; Driver function
#include <bits/stdc++.h> NEW_LINE #define maxSum  100 NEW_LINE #define arrSize  51 NEW_LINE using namespace std ; int dp [ arrSize ] [ maxSum ] ; bool visit [ arrSize ] [ maxSum ] ; int SubsetCnt ( int i , int s , int arr [ ] , int n ) { if ( i == n ) { if ( s == 0 ) return 1 ; else return 0 ; } if ( visit [ i ] [ s + maxSum ] ) return dp [ i ] [ s + maxSum ] ; visit [ i ] [ s + maxSum ] = 1 ; dp [ i ] [ s + maxSum ] = SubsetCnt ( i + 1 , s + arr [ i ] , arr , n ) + SubsetCnt ( i + 1 , s , arr , n ) ; return dp [ i ] [ s + maxSum ] ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 , -4 , -4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << SubsetCnt ( 0 , 0 , arr , n ) ; }
Number of Binary Strings of length N with K adjacent Set Bits | C ++ program to find the number of Bit Strings of length N with K adjacent set bits ; Function to find the number of Bit Strings of length N with K adjacent set bits ; Base Case when we form bit string of length n ; if f ( bit string ) = k , count this way ; Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset ; set the bit at currentIndex ; unset the bit at currentIndex ; Driver Code ; dp [ i ] [ j ] [ k ] represents bit strings of length i with f ( bit string ) = j and last bit as k ; initializer the dp ; total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE int waysToKAdjacentSetBits ( int dp [ ] [ MAX ] [ 2 ] , int n , int k , int currentIndex , int adjacentSetBits , int lastBit ) { if ( currentIndex == n ) { if ( adjacentSetBits == k ) return 1 ; return 0 ; } if ( dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] != -1 ) { return dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] ; } int noOfWays = 0 ; if ( lastBit == 1 ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits + 1 , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } else if ( ! lastBit ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] = noOfWays ; return noOfWays ; } int main ( ) { int n = 5 , k = 2 ; int dp [ MAX ] [ MAX ] [ 2 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; int totalWays = waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 0 ) ; cout << " Number ▁ of ▁ ways ▁ = ▁ " << totalWays << " STRNEWLINE " ; return 0 ; }
Tetranacci Numbers | A space optimized based CPP program to print the nth tetranacci number ; Function to print the N - th tetranacci number ; Initialize first four numbers to base cases ; declare a current variable ; Loop to add previous four numbers for each number starting from 4 and then assign first , second , third to second , third , fourth and curr to fourth respectively ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printTetra ( int n ) { if ( n < 0 ) return ; int first = 0 , second = 1 ; int third = 1 , fourth = 2 ; int curr ; if ( n == 0 ) cout << first ; else if ( n == 1 n == 2 ) cout << second ; else if ( n == 3 ) cout << fourth ; else { for ( int i = 4 ; i <= n ; i ++ ) { curr = first + second + third + fourth ; first = second ; second = third ; third = fourth ; fourth = curr ; } cout << curr ; } } int main ( ) { int n = 10 ; printTetra ( n ) ; return 0 ; }
Count ways to reach the nth stair using step 1 , 2 or 3 | A C ++ program to count number of ways to reach n 't stair when ; A recursive function used by countWays ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int countWays ( int n ) { int res [ n + 1 ] ; res [ 0 ] = 1 ; res [ 1 ] = 1 ; res [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) res [ i ] = res [ i - 1 ] + res [ i - 2 ] + res [ i - 3 ] ; return res [ n ] ; } int main ( ) { int n = 4 ; cout << countWays ( n ) ; return 0 ; }
Count ways to reach the nth stair using step 1 , 2 or 3 | A C ++ program to count number of ways to reach nth stair when ; A recursive function used by countWays ; Declaring three variables and holding the ways for first three stairs ; fourth variable ; Starting from 4 as already counted for 3 stairs ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int countWays ( int n ) { int a = 1 , b = 2 , c = 4 ; int d = 0 ; if ( n == 0 n == 1 n == 2 ) return n ; if ( n == 3 ) return c ; for ( int i = 4 ; i <= n ; i ++ ) { d = c + b + a ; a = b ; b = c ; c = d ; } return d ; } int main ( ) { int n = 4 ; cout << countWays ( n ) ; return 0 ; }
Subset Sum Problem in O ( sum ) space |  ; Initializing with 1 as sum 0 is always possible ; Loop to go through every element of the elements array ; To change the values of all possible sum values to 1 ; If sum is possible then return 1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isPossible ( int elements [ ] , int sum , int n ) { int dp [ sum + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = sum ; j >= elements [ i ] ; j -- ) { if ( dp [ j - elements [ i ] ] == 1 ) dp [ j ] = 1 ; } } if ( dp [ sum ] == 1 ) return true ; return false ; } int main ( ) { int elements [ ] = { 6 , 2 , 5 } ; int n = sizeof ( elements ) / sizeof ( elements [ 0 ] ) ; int sum = 7 ; if ( isPossible ( elements , sum , n ) ) cout << ( " YES " ) ; else cout << ( " NO " ) ; return 0 ; }
Dynamic Programming | High | A naive recursive C ++ program to find maximum tasks . ; Returns the maximum among the 2 numbers ; Returns maximum amount of task that can be done till day n ; If n is less than equal to 0 , then no solution exists ; Determines which task to choose on day n , then returns the maximum till that day ; Driver code
#include <iostream> NEW_LINE using namespace std ; int max ( int x , int y ) { return ( x > y ? x : y ) ; } int maxTasks ( int high [ ] , int low [ ] , int n ) { if ( n <= 0 ) return 0 ; return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ; } int main ( ) { int n = 5 ; int high [ ] = { 3 , 6 , 8 , 7 , 6 } ; int low [ ] = { 1 , 5 , 4 , 5 , 3 } ; cout << maxTasks ( high , low , n ) ; return 0 ; }
Binomial Coefficient | DP | C ++ program to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; function to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; base case ; C ( n , r ) = C ( n , n - r ) ; array of elements from n - r + 1 to n ; for numbers from 1 to r find arr [ j ] , such that gcd ( i , arr [ j ] ) > 1 ; if gcd > 1 , divide both by gcd ; if i becomes 1 , no need to search arr ; single pass to multiply the numerator ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int nCr ( int n , int r ) { if ( r > n ) return 0 ; if ( r > n - r ) r = n - r ; int mod = 1000000007 ; int arr [ r ] ; for ( int i = n - r + 1 ; i <= n ; i ++ ) { arr [ i + r - n - 1 ] = i ; } long int ans = 1 ; for ( int k = 1 ; k < r + 1 ; k ++ ) { int j = 0 , i = k ; while ( j < r ) { int x = gcd ( i , arr [ j ] ) ; if ( x > 1 ) { arr [ j ] /= x ; i /= x ; } if ( i == 1 ) break ; j += 1 ; } } for ( int i : arr ) ans = ( ans * i ) % mod ; return ( int ) ans ; } int main ( ) { int n = 5 , r = 2 ; cout << " Value ▁ of ▁ C ( " << n << " , ▁ " << r << " ) ▁ is ▁ " << nCr ( n , r ) << " STRNEWLINE " ; return 0 ; }
Kth character after replacing each character of String by its frequency exactly X times | C ++ program for the above approach ; Function to find the Kth character after X days ; Variable to store the KthChar ; Traverse the string ; Convert char into int ; Calculate characters ; If K is less than sum than ans = str [ i ] ; Return answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; char FindKthChar ( string str , long long K , int X ) { char ans ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int digit = str [ i ] - '0' ; int range = pow ( digit , X ) ; sum += range ; if ( K <= sum ) { ans = str [ i ] ; break ; } } return ans ; } int main ( ) { string str = "123" ; long long K = 9 ; int X = 3 ; char ans = FindKthChar ( str , K , X ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Total character pairs from two strings , with equal number of set bits in their ascii value | C ++ implementation of the approach ; Function to return the count of valid pairs ; Store frequency of number of set bits for s1 ; Store frequency of number of set bits for s2 ; Calculate total pairs ; Return the count of valid pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPairs ( string s1 , string s2 ) { int count = 0 ; int arr1 [ 7 ] , arr2 [ 7 ] ; for ( int i = 1 ; i <= 6 ; i ++ ) { arr1 [ i ] = 0 ; arr2 [ i ] = 0 ; } for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { int set_bits = __builtin_popcount ( ( int ) s1 [ i ] ) ; arr1 [ set_bits ] ++ ; } for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { int set_bits = __builtin_popcount ( ( int ) s2 [ i ] ) ; arr2 [ set_bits ] ++ ; } for ( int i = 1 ; i <= 6 ; i ++ ) count += ( arr1 [ i ] * arr2 [ i ] ) ; return count ; } int main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; cout << totalPairs ( s1 , s2 ) ; return 0 ; }
Count substrings that starts with character X and ends with character Y | C ++ implementation to count substrings starting with character X and ending with character Y ; function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstr ( string str , int n , char x , char y ) { int tot_count = 0 ; int count_x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == x ) count_x ++ ; if ( str [ i ] == y ) tot_count += count_x ; } return tot_count ; } int main ( ) { string str = " abbcaceghcak " ; int n = str . size ( ) ; char x = ' a ' , y = ' c ' ; cout << " Count ▁ = ▁ " << countSubstr ( str , n , x , y ) ; return 0 ; }
Count words in a given string | C ++ program to count no of words from given input string . ; returns number of words in str ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Move to next character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define OUT  0 NEW_LINE #define IN  1 NEW_LINE unsigned countWords ( char * str ) { int state = OUT ; unsigned wc = 0 ; while ( * str ) { if ( * str == ' ▁ ' * str == ' ' * str == ' TABSYMBOL ' ) state = OUT ; else if ( state == OUT ) { state = IN ; ++ wc ; } ++ str ; } return wc ; } int main ( void ) { char str [ ] = " One ▁ two TABSYMBOL three STRNEWLINE ▁ four TABSYMBOL five ▁ " ; cout << " No ▁ of ▁ words ▁ : ▁ " << countWords ( str ) ; return 0 ; }
Enneadecagonal number | C ++ program to find nth Enneadecagonal number ; Function to calculate Enneadecagonal number ; Formula for finding nth Enneadecagonal number ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthEnneadecagonal ( long int n ) { return ( 17 * n * n - 15 * n ) / 2 ; } int main ( ) { long int n = 6 ; cout << n << " th ▁ Enneadecagonal ▁ number ▁ : " << nthEnneadecagonal ( n ) ; return 0 ; }
Area of a Circumscribed Circle of a Square | C ++ Program to find the area of a circumscribed circle ; Utiity function ; Driver code
#include <stdio.h> NEW_LINE #define PI  3.14159265 NEW_LINE float areacircumscribed ( float a ) { return ( a * a * ( PI / 2 ) ) ; } int main ( ) { float a = 6 ; printf ( " ▁ Area ▁ of ▁ an ▁ circumscribed ▁ circle ▁ is ▁ : ▁ % .2f ▁ " , areacircumscribed ( a ) ) ; return 0 ; }
Find Nth item distributed from infinite items of infinite types based on given conditions | C ++ program for the above approach ; Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate to find the Nth day present is given out ; Find the number of presents given on day is day * ( day + 1 ) / 2 ; Iterate over the type ; Return the resultant type ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int itemType ( int n ) { int count = 0 ; int day = 1 ; while ( count + day * ( day + 1 ) / 2 < n ) { count += day * ( day + 1 ) / 2 ; day ++ ; } for ( int type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) { return type ; } } } int main ( ) { int N = 10 ; cout << itemType ( N ) ; return 0 ; }
Check if linked list is sorted ( Iterative and Recursive ) | C ++ program to check Linked List is sorted in descending order or not ; Linked list node ; function to Check Linked List is sorted in descending order or not ; Traverse the list till last node and return false if a node is smaller than or equal its next . ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; bool isSortedDesc ( struct Node * head ) { if ( head == NULL ) return true ; for ( Node * t = head ; t -> next != NULL ; t = t -> next ) if ( t -> data <= t -> next -> data ) return false ; return true ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> next = NULL ; temp -> data = data ; } int main ( ) { struct Node * head = newNode ( 7 ) ; head -> next = newNode ( 5 ) ; head -> next -> next = newNode ( 4 ) ; head -> next -> next -> next = newNode ( 3 ) ; isSortedDesc ( head ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Maximum length of consecutive 1 s or 0 s after flipping at most K characters | C ++ program for the above approach ; Function to find the maximum length continuos segment of character c after flipping at most K characters ; Stores the maximum length ; Stores the count of char ' c ' ; Start of window ; Remove the extra ' c ' from left ; Increment the value of the left ; Update the resultant maximum length of character ch ; Function to find the maximum length of consecutive 0 s or 1 s by flipping at most K characters of the string ; Print the maximum of the maximum length of 0 s or 1 s ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( string str , int n , char c , int k ) { int ans = -1 ; int cnt = 0 ; int left = 0 ; for ( int right = 0 ; right < n ; right ++ ) { if ( str [ right ] == c ) { cnt ++ ; } while ( cnt > k ) { if ( str [ left ] == c ) { cnt -- ; } left ++ ; } ans = max ( ans , right - left + 1 ) ; } return ans ; } int maxConsecutiveSegment ( string S , int K ) { int N = S . length ( ) ; return max ( maxLength ( S , N , '0' , K ) , maxLength ( S , N , '1' , K ) ) ; } int main ( ) { string S = "1001" ; int K = 1 ; cout << maxConsecutiveSegment ( S , K ) ; return 0 ; }
Minimize coins required to obtain all possible values up to N | Function to find minimum count of { 1 , 2 , 5 } valued coins required to make a change of all values in the range [ 1 , N ] ; Number of 5 valueds coins required ; Number of 1 valued coins required ; Number of 2 valued coins required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( int N ) { int T , F , O ; F = int ( ( N - 4 ) / 5 ) ; if ( ( ( N - 5 * F ) % 2 ) == 0 ) { O = 2 ; } else { O = 1 ; } T = floor ( ( N - 5 * F - O ) / 2 ) ; cout << " Count ▁ of ▁ 5 ▁ valueds ▁ coins : ▁ " << F << endl ; cout << " Count ▁ of ▁ 2 ▁ valueds ▁ coins : ▁ " << T << endl ; cout << " Count ▁ of ▁ 1 ▁ valueds ▁ coins : ▁ " << O << endl ; } int main ( ) { int N = 8 ; find ( N ) ; return 0 ; }
Replace ' ? ' to convert given string to a binary string with maximum count of '0' and "10" | C ++ program to implement the above approach ; Function to maximize count of 0 and 10 by replacing character ' ? ' to '0' or '1' ; Traverse the given string ; If current character is ' ? ' ; Replace str [ i ] to '0' ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxOccurence ( string str , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ? ' ) { str [ i ] = '0' ; } } cout << str << endl ; } int main ( ) { string str = "10?0?11" ; int N = str . length ( ) ; findMaxOccurence ( str , N ) ; return 0 ; }