codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Sum Increasing Subsequence | DP | Dynamic Programming implementation of Maximum Sum Increasing Subsequence ( MSIS ) problem ; maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumIS ( int arr [ ] , int n ) { int i , j , max = 0 ; int msis [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( max < msis [ i ] ) max = msis [ i ] ; return max ; } int main ( ) { int arr [ ] = { 1 , 101 , 2 , 3 , 100 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ " " subsequence ▁ is ▁ " << maxSumIS ( arr , n ) << endl ; return 0 ; }
C / C ++ Program for Longest Increasing Subsequence | A Naive C ++ recursive implementation of LIS problem ; To make use of recursive calls , this function must return two things : 1 ) Length of LIS ending with element arr [ n - 1 ] . We use max_ending_here for this purpose 2 ) Overall maximum as the LIS may end with an element before arr [ n - 1 ] max_ref is used this purpose . The value of LIS of full array of size n is stored in * max_ref which is our final result ; Base case ; ' max _ ending _ here ' is length of LIS ending with arr [ n - 1 ] ; Recursively get all LIS ending with arr [ 0 ] , arr [ 1 ] ... arr [ n - 2 ] . If arr [ i - 1 ] is smaller than arr [ n - 1 ] , and max ending with arr [ n - 1 ] needs to be updated , then update it ; Compare max_ending_here with the overall max . And update the overall max if needed ; Return length of LIS ending with arr [ n - 1 ] ; The wrapper function for _lis ( ) ; The max variable holds the result ; The function _lis ( ) stores its result in max ; returns max ; Driver code
#include <iostream> NEW_LINE using namespace std ; int _lis ( int arr [ ] , int n , int * max_ref ) { if ( n == 1 ) return 1 ; int res , max_ending_here = 1 ; for ( int i = 1 ; i < n ; i ++ ) { res = _lis ( arr , i , max_ref ) ; if ( arr [ i - 1 ] < arr [ n - 1 ] && res + 1 > max_ending_here ) max_ending_here = res + 1 ; } if ( * max_ref < max_ending_here ) * max_ref = max_ending_here ; return max_ending_here ; } int lis ( int arr [ ] , int n ) { int max = 1 ; _lis ( arr , n , & max ) ; return max ; } int main ( ) { int arr [ ] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ of ▁ lis ▁ is ▁ " << lis ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Find position i to split Array such that prefix sum till i | C ++ program for the above approach ; Function to check if there is an element forming G . P . series having common ratio k ; If size of array is less than three then return - 1 ; Initialize the variables ; Calculate total sum of array ; Calculate Middle element of G . P . series ; Iterate over the range ; Store the first element of G . P . series in the variable temp ; Return position of middle element of the G . P . series if the first element is in G . P . of common ratio k ; Else return 0 ; if middle element is not found in arr [ ] ; Driver Code ; Given array
#include <iostream> NEW_LINE using namespace std ; int checkArray ( int arr [ ] , int N , int k ) { if ( N < 3 ) return -1 ; int i , Sum = 0 , temp = 0 ; for ( i = 0 ; i < N ; i ++ ) Sum += arr [ i ] ; int R = ( k * k + k + 1 ) ; if ( Sum % R != 0 ) return 0 ; int Mid = k * ( Sum / R ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { temp += arr [ i - 1 ] ; if ( arr [ i ] == Mid ) { if ( temp == Mid / k ) return i + 1 ; else return 0 ; } } return 0 ; } int main ( ) { int arr [ ] = { 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << checkArray ( arr , N , K ) << endl ; return 0 ; }
Repeat last occurrence of each alphanumeric character to their position in character family times | C ++ program for the above approach ; Function to encode the given string ; Variable string to store the result ; Arrays to store the last occuring index of every character in the string ; Length of the string ; Iterate over the range ; If str [ i ] is between 0 and 9 ; If str [ i ] is between a and z ; If str [ i ] is between A and Z ; Iterate over the range ; If str [ i ] is between a and z and i is the last occurence in str ; If str [ i ] is between A and Z and i is the last occurence in str ; If str [ i ] is between 0 and 9 and i is the last occurence in str ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void encodeString ( string str ) { string res = " " ; int small [ 26 ] = { 0 } , capital [ 26 ] = { 0 } , num [ 10 ] = { 0 } ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) { num [ str [ i ] - 48 ] = i ; } else if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { small [ str [ i ] - 97 ] = i ; } else if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { capital [ str [ i ] - 65 ] = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) && small [ str [ i ] - 97 ] == i ) { int occ = str [ i ] - 96 ; while ( occ -- ) { res += str [ i ] ; } } else if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) && capital [ str [ i ] - 65 ] == i ) { int occ = str [ i ] - 64 ; while ( occ -- ) { res += str [ i ] ; } } else if ( ( str [ i ] >= '0' && str [ i ] <= '9' ) && num [ str [ i ] - 48 ] == i ) { int occ = str [ i ] - 48 ; while ( occ -- ) { res += str [ i ] ; } } else { res += str [ i ] ; } } cout << res ; } int main ( ) { string str = " Ea2 , ▁ 0 , ▁ E " ; encodeString ( str ) ; return 0 ; }
Form minimum number of Palindromic Strings from a given string | C ++ Program to implement the above approach ; Function to return minimum palindromic strings formed ; Store the length of string ; Iterate over the string and store the frequency of all charactrs A vector to store the frequency ; Store the odd frequency characters ; Iterate through all possible characters and check the parity of frequency ; store answer in a vector ; if there are no odd Frequency characters then print the string with even characters ; store the left part of the palindromic string ; reverse the right part of the string ; store the string in ans vector ; take any character from off frequency element ; repeat the above step to form left and right strings ; reverse the right part of the string ; store the string in ans vector ; store all other odd frequency strings ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumPalindromicStrings ( string S ) { int N = S . length ( ) ; vector < int > freq ( 26 ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S [ i ] - ' a ' ] ++ ; } vector < int > oddFreqCharacters ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] & 1 ) { oddFreqCharacters . push_back ( i ) ; freq [ i ] -- ; } freq [ i ] /= 2 ; } vector < string > ans ; if ( oddFreqCharacters . empty ( ) ) { string left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += char ( i + ' a ' ) ; } } string right = left ; reverse ( right . begin ( ) , right . end ( ) ) ; ans . push_back ( left + right ) ; } else { string middle = " " ; int c = oddFreqCharacters . back ( ) ; oddFreqCharacters . pop_back ( ) ; middle += char ( c + ' a ' ) ; string left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += char ( i + ' a ' ) ; } } string right = left ; reverse ( right . begin ( ) , right . end ( ) ) ; ans . push_back ( left + middle + right ) ; while ( ! oddFreqCharacters . empty ( ) ) { int c = oddFreqCharacters . back ( ) ; oddFreqCharacters . pop_back ( ) ; string middle = " " ; middle += char ( c + ' a ' ) ; ans . push_back ( middle ) ; } } cout << " [ " ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] ; if ( i != ans . size ( ) - 1 ) { cout << " , ▁ " ; } } cout << " ] " ; return 0 ; } int main ( ) { string S = " geeksforgeeks " ; minimumPalindromicStrings ( S ) ; }
Make Array elements equal by replacing adjacent elements with their XOR | C ++ Program of the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleEqualArray ( int A [ ] , int N ) { vector < int > pref ( N ) ; pref [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { cout << " YES " ; return ; } int cur_xor = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { if ( j ) { int middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; int left_xor = pref [ j - 1 ] ; int right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { cout << " YES " ; return ; } } } } cout << " NO " ; } int main ( ) { int A [ ] = { 0 , 2 , 2 } ; int N = sizeof ( A ) / sizeof ( int ) ; possibleEqualArray ( A , N ) ; return 0 ; }
Construct an array whose Prefix XOR array starting from X is an N | C ++ program for the above approach ; Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void GenerateArray ( int N , int X ) { int prev_xor = X ; for ( int i = 1 ; i <= N ; i ++ ) { cout << ( i ^ prev_xor ) ; if ( i != N ) { cout << " ▁ " ; } prev_xor = i ; } } int main ( ) { int N = 4 , X = 3 ; cout << " The ▁ generated ▁ array ▁ is ▁ " ; GenerateArray ( N , X ) ; return 0 ; }
Check if elements of a Binary Matrix can be made alternating | C ++ program for the above approach ; Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createGrid ( char grid [ ] [ 1001 ] , bool is1 , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( is1 ) { grid [ i ] [ j ] = '0' ; is1 = false ; } else { grid [ i ] [ j ] = '1' ; is1 = true ; } } if ( M % 2 == 0 ) is1 = ! is1 ; } } bool testGrid ( char testGrid [ ] [ 1001 ] , char Grid [ ] [ 1001 ] , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( Grid [ i ] [ j ] != ' * ' ) { if ( Grid [ i ] [ j ] != testGrid [ i ] [ j ] ) { return false ; } } } } return true ; } void printGrid ( char grid [ ] [ 1001 ] , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cout << grid [ i ] [ j ] << " ▁ " ; } cout << endl ; } } void findPossibleGrid ( int N , int M , char grid [ ] [ 1001 ] ) { char gridTest1 [ N ] [ 1001 ] , gridTest2 [ N ] [ 1001 ] ; createGrid ( gridTest1 , true , N , M ) ; createGrid ( gridTest2 , false , N , M ) ; if ( testGrid ( gridTest1 , grid , N , M ) ) { cout << " Yes STRNEWLINE " ; printGrid ( gridTest1 , N , M ) ; } else if ( testGrid ( gridTest2 , grid , N , M ) ) { cout << " Yes STRNEWLINE " ; printGrid ( gridTest2 , N , M ) ; } else { cout << " No STRNEWLINE " ; } } int main ( ) { int N = 4 , M = 4 ; char grid [ ] [ 1001 ] = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( N , M , grid ) ; return 0 ; }
Find non | C ++ program for the above approach . ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long long INF64 = 1000000000000000000ll ; const int N = 200 * 1000 + 13 ; int n ; long long arr [ N ] , brr [ N ] ; void brute ( int ind , long long l , long long r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < int ( n ) ; i ++ ) printf ( " % lld ▁ " , brr [ i ] ) ; puts ( " " ) ; exit ( 0 ) ; } for ( long long i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } int main ( ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; return 0 ; }
Find and Replace all occurrence of a substring in the given String | C ++ program for the above approach ; Function to replace all the occurrences of the substring S1 to S2 in string S ; Stores the resultant string ; Traverse the string s ; If the first character of string s1 matches with the current character in string s ; If the complete string matches or not ; If complete string matches then replace it with the string s2 ; Otherwise ; Otherwise ; Print the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void modifyString ( string & s , string & s1 , string & s2 ) { string ans = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int k = 0 ; if ( s [ i ] == s1 [ k ] && i + s1 . length ( ) <= s . length ( ) ) { int j ; for ( j = i ; j < i + s1 . length ( ) ; j ++ ) { if ( s [ j ] != s1 [ k ] ) { break ; } else { k = k + 1 ; } } if ( j == i + s1 . length ( ) ) { ans . append ( s2 ) ; i = j - 1 ; } else { ans . push_back ( s [ i ] ) ; } } else { ans . push_back ( s [ i ] ) ; } } cout << ans ; } int main ( ) { string S = " geeksforgeeks " ; string S1 = " eek " ; string S2 = " ok " ; modifyString ( S , S1 , S2 ) ; return 0 ; }
Count of minimum numbers having K as the last digit required to obtain sum N | C ++ program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCount ( int N , int K ) { int SmallestNumber [ 10 ] ; int MinimumSteps [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = INT_MAX ; MinimumSteps [ i ] = INT_MAX ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return -1 ; } else { return MinimumSteps [ N % 10 ] ; } } int main ( ) { int N = 42 , K = 7 ; cout << minCount ( N , K ) ; return 0 ; }
Minimum number of operations required to make an array non | C ++ program for the above approach ; Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinSteps ( int arr [ ] , int N ) { int val = 0 ; int mx = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = max ( mx , curr ) ; val = max ( val , mx - curr ) ; } long long res = 0 ; while ( ( 1LL << res ) - 1 < val ) { ++ res ; } return res ; } int main ( ) { int arr [ ] = { 1 , 7 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countMinSteps ( arr , N ) ; return 0 ; }
Minimum XOR of at most K elements in range [ L , R ] | C ++ program for the above approach ; Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int func2 ( int L , int R , int K ) { if ( R - L >= 2 ) return 1 ; return min ( L , L ^ R ) ; } int func3 ( int L , int R , int K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } int func4 ( int L , int R , int K ) { if ( R - L >= 4 ) return 0 ; int minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return min ( minval , func3 ( L , R , K ) ) ; } int minimumXor ( int L , int R , int K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } int main ( ) { int L = 1 , R = 3 , K = 3 ; cout << minimumXor ( L , R , K ) << endl ; return 0 ; }
Nth term of Ruler Function Series | C ++ program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; cout << x ; } int main ( ) { int N = 8 ; findNthTerm ( N ) ; return 0 ; }
Quadratic equation whose roots are reciprocal to the roots of given equation | C ++ program for the above approach ; Function to find the quadratic equation having reciprocal roots ; Print quadratic equation ; Driver Code ; Given coefficients ; Function call to find the quadratic equation having reciprocal roots
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int A , int B , int C ) { cout << " ( " << C << " ) " << " x ^ 2 ▁ + ( " << B << " ) x ▁ + ▁ ( " << A << " ) ▁ = ▁ 0" ; } int main ( ) { int A = 1 , B = -5 , C = 6 ; findEquation ( A , B , C ) ; return 0 ; }
Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | C ++ program for the above approach ; Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinMoves ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % N != 0 ) return -1 ; int avg = sum / N ; int total = 0 ; int needCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { needCount += ( arr [ i ] - avg ) ; total = max ( max ( abs ( needCount ) , arr [ i ] - avg ) , total ) ; } return total ; } int main ( ) { int arr [ ] = { 1 , 0 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinMoves ( arr , N ) ; return 0 ; }
Maximum amount of money that can be collected by a player in a game of coins | CPP Program to implement the above approach ; Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver Code ; Function call to calculate the amount of coins collected by A
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( int N , vector < vector < int > > Arr ) { int amount = 0 ; vector < int > mid_odd ; for ( int i = 0 ; i < N ; i ++ ) { int siz = Arr [ i ] . size ( ) ; for ( int j = 0 ; j < siz / 2 ; j ++ ) amount = amount + Arr [ i ] [ j ] ; if ( siz % 2 == 1 ) mid_odd . push_back ( Arr [ i ] [ siz / 2 ] ) ; } sort ( mid_odd . begin ( ) , mid_odd . end ( ) ) ; for ( int i = 0 ; i < mid_odd . size ( ) ; i ++ ) if ( i % 2 == 0 ) amount = amount + mid_odd [ i ] ; cout << amount << endl ; } int main ( ) { int N = 2 ; vector < vector < int > > Arr { { 5 , 2 , 3 , 4 } , { 1 , 6 } } ; find ( N , Arr ) ; }
Check if an array can be split into K consecutive non | C ++ program for the above approach ; Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkPattern ( int arr [ ] , int m , int k , int n ) { int count = 1 , t = 0 ; for ( int i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " Yes " ; } } } else { t = 0 ; count = 1 ; } } return " No " ; } int main ( ) { int arr [ ] = { 6 , 1 , 3 , 3 , 3 , 3 } ; int M = 1 , K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << checkPattern ( arr , M , K , N ) ; return 0 ; }
Remove all occurrences of a word from a given string using Z | C ++ program for the above approach ; Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If substring match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated string " P $ T " ; Store Z array of concat ; Stores string , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getZarr ( string str , int Z [ ] ) { int n = str . length ( ) ; int k ; int L = 0 ; int R = 0 ; for ( int i = 1 ; i < n ; ++ i ) { if ( i > R ) { L = R = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } else { k = i - L ; if ( Z [ k ] < R - i + 1 ) { Z [ i ] = Z [ k ] ; } else { L = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } } } } string goodStr ( string str , string word ) { string concat = word + " $ " + str ; int l = concat . length ( ) ; int Z [ l ] ; getZarr ( concat , Z ) ; string res ; int pSize = word . size ( ) ; for ( int i = 0 ; i < l ; ++ i ) { if ( i + pSize < l - 1 && Z [ i + pSize + 1 ] == pSize ) { i += pSize - 1 ; } else if ( i < str . length ( ) ) { res += str [ i ] ; } } return res ; } int main ( ) { string str = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching " ; string word = " km " ; cout << goodStr ( str , word ) ; return 0 ; }