codeparrot/xlcost-text-to-code huggingface dataset:
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
def findString ( S , N ) : NEW_LINE INDENT strLen = 4 * N NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT S [ strLen - 1 ] = '1' NEW_LINE strLen -= 2 NEW_LINE DEDENT for i in range ( 4 * N ) : NEW_LINE INDENT print ( S [ i ] , end = " " ) NEW_LINE DEDENT DEDENT N = 2 NEW_LINE S = [ 0 ] * ( 4 * N ) NEW_LINE for i in range ( 4 * N ) : NEW_LINE INDENT S [ i ] = '0' NEW_LINE DEDENT findString ( S , N ) NEW_LINE
Check if a string can be split into two substrings with equal number of vowels | 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
def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def containsEqualStrings ( S ) : NEW_LINE INDENT totalVowels = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( isVowel ( S [ i ] ) ) : NEW_LINE INDENT totalVowels += 1 NEW_LINE DEDENT DEDENT vowelsTillNow = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( isVowel ( S [ i ] ) ) : NEW_LINE INDENT vowelsTillNow += 1 NEW_LINE totalVowels -= 1 NEW_LINE if ( vowelsTillNow == totalVowels ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeks " NEW_LINE print ( containsEqualStrings ( S ) ) NEW_LINE DEDENT
Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | Python3 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
import sys NEW_LINE def getMaxSum ( A , M , N ) : NEW_LINE INDENT sum = 0 NEW_LINE negative = 0 NEW_LINE minVal = sys . maxsize NEW_LINE for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT sum += abs ( A [ i ] [ j ] ) NEW_LINE if ( A [ i ] [ j ] < 0 ) : NEW_LINE INDENT negative += 1 NEW_LINE DEDENT minVal = min ( minVal , abs ( A [ i ] [ j ] ) ) NEW_LINE DEDENT DEDENT if ( negative % 2 ) : NEW_LINE INDENT sum -= 2 * minVal NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 4 , - 8 , 6 ] , [ 3 , 7 , 2 ] ] NEW_LINE M = len ( A ) NEW_LINE N = len ( A [ 0 ] ) NEW_LINE getMaxSum ( A , M , N ) NEW_LINE DEDENT
Calculate total wall area of houses painted | 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
def areaToPaint ( N , W , L , Heights ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT total += 2 * Heights [ i ] * W NEW_LINE DEDENT total += L * ( Heights [ 0 ] + Heights [ N - 1 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT total += L * abs ( Heights [ i ] - Heights [ i - 1 ] ) NEW_LINE DEDENT print ( total ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE W = 1 NEW_LINE L = 1 NEW_LINE Heights = [ 4 , 3 , 1 , 2 , 3 , 4 , 2 ] NEW_LINE areaToPaint ( N , W , L , Heights ) NEW_LINE DEDENT
Maximize sum of count of distinct prime factors of K array elements | Python 3 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
MAX = 1000000 NEW_LINE def maxSumOfDistinctPrimeFactors ( arr , N , K ) : NEW_LINE INDENT CountDistinct = [ 0 ] * ( MAX + 1 ) NEW_LINE prime = [ False ] * ( MAX + 1 ) NEW_LINE for i in range ( MAX + 1 ) : NEW_LINE INDENT CountDistinct [ i ] = 0 NEW_LINE prime [ i ] = True NEW_LINE DEDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT CountDistinct [ i ] = 1 NEW_LINE for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT CountDistinct [ j ] += 1 NEW_LINE prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE PrimeFactor = [ 0 ] * 20 NEW_LINE for i in range ( N ) : NEW_LINE INDENT PrimeFactor [ CountDistinct [ arr [ i ] ] ] += 1 NEW_LINE DEDENT for i in range ( 19 , 0 , - 1 ) : NEW_LINE INDENT while ( PrimeFactor [ i ] > 0 ) : NEW_LINE INDENT sum += i NEW_LINE PrimeFactor [ i ] -= 1 NEW_LINE K -= 1 NEW_LINE if ( K == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( K == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 9 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE maxSumOfDistinctPrimeFactors ( arr , N , K ) NEW_LINE DEDENT
Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | Function to count the changes required such that no palindromic subof length exceeding 1 is present in the string ;  ; Stores the count ; Iterate over the string ; Palindromic Subof Length 2 ; Replace the next character ; Increment changes ; Palindromic Subof Length 3 ; Replace the next character ; Increment changes ; Driver Code
def maxChange ( str ) : NEW_LINE INDENT str = [ i for i in str ] NEW_LINE DEDENT / * Base Case * / NEW_LINE INDENT if ( len ( str ) <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT minChanges = 0 NEW_LINE for i in range ( len ( str ) - 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT str [ i + 1 ] = ' N ' NEW_LINE minChanges += 1 NEW_LINE DEDENT elif ( i > 0 and str [ i - 1 ] == str [ i + 1 ] ) : NEW_LINE INDENT str [ i + 1 ] = ' N ' NEW_LINE minChanges += 1 NEW_LINE DEDENT DEDENT return minChanges NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " bbbbbbb " NEW_LINE print ( maxChange ( str ) ) NEW_LINE DEDENT
Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; Iterator to store first element of B ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver Code ; Given array ; Given size ; Print the required smallest integer
def findMinKToMakeAllEqual ( N , A ) : NEW_LINE INDENT B = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT B [ A [ i ] ] = 1 NEW_LINE DEDENT M = len ( B ) NEW_LINE itr , i = list ( B . keys ( ) ) , 0 NEW_LINE if ( M > 3 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT elif ( M == 3 ) : NEW_LINE INDENT B_1 , i = itr [ i ] , i + 1 NEW_LINE B_2 , i = itr [ i ] , i + 1 NEW_LINE B_3 , i = itr [ i ] , i + 1 NEW_LINE if ( B_2 - B_1 == B_3 - B_2 ) : NEW_LINE INDENT print ( B_2 - B_1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT elif ( M == 2 ) : NEW_LINE INDENT B_1 , i = itr [ i ] , i + 1 NEW_LINE B_2 , i = itr [ i ] , i + 1 NEW_LINE if ( ( B_2 - B_1 ) % 2 == 0 ) : NEW_LINE INDENT print ( ( B_2 - B_1 ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( B_2 - B_1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 3 , 5 , 1 ] NEW_LINE N = len ( A ) NEW_LINE findMinKToMakeAllEqual ( N , A ) NEW_LINE DEDENT
Maximum number of times a given string needs to be concatenated to form a substring of another string | Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which S2 concatenated K times is a subof S1 ; Store the required maximum number ; Create a temporary to store word ; Store the maximum number of times S2 can occur in S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call
def computeLPSArray ( pat , M , lps ) : NEW_LINE INDENT lenn = 0 NEW_LINE lps [ 0 ] = 0 NEW_LINE i = 1 NEW_LINE while ( i < M ) : NEW_LINE INDENT if ( pat [ i ] == pat [ lenn ] ) : NEW_LINE INDENT lenn += 1 NEW_LINE lps [ i ] = lenn NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( lenn != 0 ) : NEW_LINE INDENT lenn = lps [ lenn - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lps [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def KMPSearch ( pat , txt ) : NEW_LINE INDENT M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE lps = [ 0 for i in range ( M ) ] NEW_LINE computeLPSArray ( pat , M , lps ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT if ( pat [ j ] == txt [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( j == M ) : NEW_LINE INDENT return 1 NEW_LINE j = lps [ j - 1 ] NEW_LINE DEDENT elif ( i < N and pat [ j ] != txt [ i ] ) : NEW_LINE INDENT if ( j != 0 ) : NEW_LINE INDENT j = lps [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT def maxRepeating ( seq , word ) : NEW_LINE INDENT resCount = 0 NEW_LINE curWord = word NEW_LINE numWords = len ( seq ) // len ( word ) NEW_LINE for i in range ( numWords ) : NEW_LINE INDENT if ( KMPSearch ( curWord , seq ) ) : NEW_LINE INDENT curWord += word NEW_LINE resCount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( resCount ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 , S2 = " ababc " , " ab " NEW_LINE maxRepeating ( S1 , S2 ) NEW_LINE DEDENT
Count all possible strings that can be generated by placing spaces | Function to count the number of strings that can be generated by placing spaces between pair of adjacent characters ; Length of the string ; Count of positions for spaces ; Count of possible strings ; Driver Code
def countNumberOfStrings ( s ) : NEW_LINE INDENT length = len ( s ) NEW_LINE n = length - 1 NEW_LINE count = 2 ** n NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ABCD " NEW_LINE print ( countNumberOfStrings ( S ) ) NEW_LINE DEDENT
Minimum time required to reach a given score | Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Prthe time required ; Driver Code ; Function Call
def findMinimumTime ( p , n , target ) : NEW_LINE INDENT um = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT um [ p [ i ] ] = um . get ( p [ i ] , 0 ) + 1 NEW_LINE DEDENT time = 0 NEW_LINE sum = 0 NEW_LINE while ( sum < target ) : NEW_LINE INDENT sum = 0 NEW_LINE time += 1 NEW_LINE for it in um : NEW_LINE INDENT sum += um [ it ] * ( time // it ) NEW_LINE DEDENT DEDENT print ( time ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE target = 10 NEW_LINE findMinimumTime ( arr , N , target ) NEW_LINE DEDENT
Construct MEX array from the given array | Python3 program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Prthe array B ; Driver Code ; Given array ; Size of array ; Function call
MAXN = 100001 NEW_LINE def constructMEX ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * MAXN NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] = 1 NEW_LINE DEDENT MexOfArr = 0 NEW_LINE for i in range ( 1 , MAXN ) : NEW_LINE INDENT if ( hash [ i ] == 0 ) : NEW_LINE INDENT MexOfArr = i NEW_LINE break NEW_LINE DEDENT DEDENT B = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < MexOfArr ) : NEW_LINE INDENT B [ i ] = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT B [ i ] = MexOfArr NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE constructMEX ( arr , N ) NEW_LINE DEDENT
Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | Function to print smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of arr [ ] in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array arr [ ] ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
def smallestNumberLeftInPQ ( arr , N ) : NEW_LINE INDENT pq = [ ] NEW_LINE pairsArr = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( arr [ i ] ) NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE while ( len ( pq ) > 1 ) : NEW_LINE INDENT X = pq [ - 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE Y = pq [ - 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE pq . append ( ( X + Y + 1 ) // 2 ) NEW_LINE pairsArr . append ( [ X , Y ] ) NEW_LINE pq = sorted ( pq ) NEW_LINE DEDENT print ( " { " , pq [ - 1 ] , " } , ▁ " , end = " " ) NEW_LINE sz = len ( pairsArr ) NEW_LINE for i in range ( sz ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT print ( " { ▁ " , end = " " ) NEW_LINE DEDENT print ( " ( " , pairsArr [ i ] [ 0 ] , " , " , pairsArr [ i ] [ 1 ] , " ) " , end = " " ) NEW_LINE if ( i != sz - 1 ) : NEW_LINE INDENT print ( end = " , ▁ " ) NEW_LINE DEDENT if ( i == sz - 1 ) : NEW_LINE INDENT print ( end = " ▁ } " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE smallestNumberLeftInPQ ( arr , N ) NEW_LINE DEDENT
Move weighting scale alternate under given constraints | DFS method to traverse among states of weighting scales ; If we reach to more than required steps , return true ; Try all possible weights and choose one which returns 1 afterwards ; Try this weight only if it is greater than current residueand not same as previous chosen weight ; assign this weight to array and recur for next state ; if any weight is not possible , return false ; method prints weights for alternating scale and if not possible prints ' not ▁ possible ' ; call dfs with current residue as 0 and current steps as 0 ; Driver Code
def dfs ( residue , curStep , wt , arr , N , steps ) : NEW_LINE INDENT if ( curStep >= steps ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > residue and arr [ i ] != wt [ curStep - 1 ] ) : NEW_LINE INDENT wt [ curStep ] = arr [ i ] NEW_LINE if ( dfs ( arr [ i ] - residue , curStep + 1 , wt , arr , N , steps ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def printWeightsOnScale ( arr , N , steps ) : NEW_LINE INDENT wt = [ 0 ] * ( steps ) NEW_LINE if ( dfs ( 0 , 0 , wt , arr , N , steps ) ) : NEW_LINE INDENT for i in range ( steps ) : NEW_LINE INDENT print ( wt [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE steps = 10 NEW_LINE printWeightsOnScale ( arr , N , steps ) NEW_LINE DEDENT
Minimum removals required such that sum of remaining array modulo M is X | Python3 program for the above approach ; Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Driver Code ; Given array ; Given size ; Given mod and x ; Function call
import sys NEW_LINE def findSum ( S , n , x ) : NEW_LINE INDENT table = [ [ 0 for x in range ( x + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , x + 1 ) : NEW_LINE INDENT table [ 0 ] [ i ] = sys . maxsize - 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , x + 1 ) : NEW_LINE INDENT if ( S [ i - 1 ] > j ) : NEW_LINE INDENT table [ i ] [ j ] = table [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT table [ i ] [ j ] = min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT if ( table [ n ] [ x ] > n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return table [ n ] [ x ] NEW_LINE DEDENT def minRemovals ( arr , n , m , x ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT requied_Sum = 0 NEW_LINE if ( sum % m < x ) : NEW_LINE INDENT requied_Sum = m + sum % m - x NEW_LINE DEDENT else : NEW_LINE INDENT requied_Sum = sum % m - x NEW_LINE DEDENT print ( findSum ( arr , n , requied_Sum ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE m = 4 NEW_LINE x = 2 NEW_LINE minRemovals ( arr , n , m , x % m ) NEW_LINE DEDENT
Count minimum character replacements required such that given string satisfies the given conditions | Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Driver Code ; Given string S ; Function Call
def minChange ( s , n ) : NEW_LINE INDENT L = [ 0 ] * 26 ; NEW_LINE R = [ 0 ] * 26 ; NEW_LINE for i in range ( 0 , n // 2 ) : NEW_LINE INDENT ch = s [ i ] ; NEW_LINE L [ ord ( ch ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( n // 2 , n ) : NEW_LINE INDENT ch = s [ i ] ; NEW_LINE R [ ord ( ch ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT count = n ; NEW_LINE for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) ) : NEW_LINE INDENT count = min ( count , n - L [ ch - ord ( ' a ' ) ] - R [ ch - ord ( ' a ' ) ] ) ; NEW_LINE DEDENT change = n / 2 ; NEW_LINE for d in range ( 0 , 25 ) : NEW_LINE INDENT change -= L [ d ] ; NEW_LINE change += R [ d ] ; NEW_LINE count = min ( count , change ) ; NEW_LINE DEDENT change = n / 2 ; NEW_LINE for d in range ( 0 , 25 ) : NEW_LINE INDENT change -= R [ d ] ; NEW_LINE change += L [ d ] ; NEW_LINE count = min ( change , count ) ; NEW_LINE DEDENT return int ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aababc " ; NEW_LINE N = len ( S ) ; NEW_LINE print ( minChange ( S , N ) ) ; NEW_LINE DEDENT
Rearrange string to obtain Longest Palindromic Substring | Function to rearrange the string to get the longest palindromic substring ; Stores the length of str ; Store the count of occurrence of each character ; Traverse the string , str ; Count occurrence of each character ; Store the left half of the longest palindromic substring ; Store the right half of the longest palindromic substring ; Traverse the array , hash [ ] ; Append half of the characters to res1 ; Append half of the characters to res2 ; reverse string res2 to make res1 + res2 palindrome ; Store the remaining characters ; Check If any odd character appended to the middle of the resultant string or not ; Append all the character which occurs odd number of times ; If count of occurrence of characters is odd ; Driver Code
def longestPalinSub ( st ) : NEW_LINE INDENT N = len ( st ) NEW_LINE hash1 = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash1 [ ord ( st [ i ] ) ] += 1 NEW_LINE DEDENT res1 = " " NEW_LINE res2 = " " NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT for j in range ( hash1 [ i ] // 2 ) : NEW_LINE INDENT res1 += chr ( i ) NEW_LINE DEDENT for j in range ( ( hash1 [ i ] + 1 ) // 2 , hash1 [ i ] ) : NEW_LINE INDENT res2 += chr ( i ) NEW_LINE DEDENT DEDENT p = list ( res2 ) NEW_LINE p . reverse ( ) NEW_LINE res2 = ' ' . join ( p ) NEW_LINE res3 = " " NEW_LINE f = False NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT if ( hash1 [ i ] % 2 ) : NEW_LINE INDENT if ( not f ) : NEW_LINE INDENT res1 += chr ( i ) NEW_LINE f = True NEW_LINE DEDENT else : NEW_LINE INDENT res3 += chr ( i ) NEW_LINE DEDENT DEDENT DEDENT return ( res1 + res2 + res3 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE print ( longestPalinSub ( st ) ) NEW_LINE DEDENT