codeparrot/xlcost-text-to-code huggingface dataset:
Count of unique digits in a given number N | Function that returns the count of unique digits of the given number ; Initialize a variable to store count of unique digits ; Initialize cnt list to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Iterate through the cnt list ; If frequency of digit is 1 ; Increment the count of unique digits ; Return the count of unique digit ; Given number N ; Function call
def countUniqueDigits ( N ) : NEW_LINE INDENT res = 0 NEW_LINE cnt = [ 0 ] * 10 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT rem = N % 10 NEW_LINE cnt [ rem ] += 1 NEW_LINE N = N // 10 NEW_LINE DEDENT for i in range ( 10 ) : NEW_LINE INDENT if ( cnt [ i ] == 1 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT N = 2234262 NEW_LINE print ( countUniqueDigits ( N ) ) NEW_LINE
Check if frequency of each element in given array is unique or not | Python3 code for the above approach ; Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Driver Code ; Given array arr [ ] ; Function Call ; Print the result
from collections import defaultdict NEW_LINE def checkUniqueFrequency ( arr , n ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT uniqueFreq = set ( [ ] ) NEW_LINE for i in freq : NEW_LINE INDENT if ( freq [ i ] in uniqueFreq ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT uniqueFreq . add ( freq [ i ] ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE res = checkUniqueFrequency ( arr , n ) NEW_LINE if ( res ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Sum of first N natural numbers with all powers of 2 added twice | Python3 program to implement the above approach ; Function to raise N to the power P and return the value ; Function to calculate the log base 2 of an integer ; Calculate log2 ( N ) indirectly using log ( ) method ; Function to calculate and return the required sum ; Sum of first N natural numbers ; Sum of all powers of 2 up to N ; Driver code
import math NEW_LINE def power ( N , P ) : NEW_LINE INDENT return math . pow ( N , P ) NEW_LINE DEDENT def Log2 ( N ) : NEW_LINE INDENT result = ( math . log ( N ) // math . log ( 2 ) ) NEW_LINE return result NEW_LINE DEDENT def specialSum ( n ) : NEW_LINE INDENT sum = n * ( n + 1 ) // 2 NEW_LINE a = Log2 ( n ) NEW_LINE sum = sum + power ( 2 , a + 1 ) - 1 NEW_LINE return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( specialSum ( n ) ) NEW_LINE DEDENT
Rearrange an array such that product of every two consecutive elements is a multiple of 4 | Function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; If element is odd ; Odd ; If element is divisible by 4 ; Divisible by 4 ; If element is not divisible by 4 ; Even but not divisible by 4 ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Print the NON_FOUR [ i ] elements at the end ; No possible configuration ; Driver Code
def Permute ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE four = 0 NEW_LINE non_four = 0 NEW_LINE ODD , FOUR , NON_FOUR = [ ] , [ ] , [ ] NEW_LINE for x in arr : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE ODD . append ( x ) NEW_LINE DEDENT elif ( x % 4 == 0 ) : NEW_LINE INDENT four += 1 NEW_LINE FOUR . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT non_four += 1 NEW_LINE NON_FOUR . append ( x ) NEW_LINE DEDENT DEDENT if ( non_four == 0 and four >= odd - 1 ) : NEW_LINE INDENT x = len ( ODD ) NEW_LINE y = len ( FOUR ) NEW_LINE i = 0 NEW_LINE while i < x : NEW_LINE INDENT print ( ODD [ i ] , end = " ▁ " ) NEW_LINE if ( i < y ) : NEW_LINE INDENT print ( FOUR [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT while ( i < y ) : NEW_LINE INDENT print ( FOUR [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT elif ( non_four > 0 and four >= odd ) : NEW_LINE INDENT x = len ( ODD ) NEW_LINE y = len ( FOUR ) NEW_LINE i = 0 NEW_LINE while i < x : NEW_LINE INDENT print ( ODD [ i ] , end = " ▁ " ) NEW_LINE if ( i < y ) : NEW_LINE INDENT print ( FOUR [ i ] , end = " ▁ " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT while ( i < y ) : NEW_LINE INDENT print ( FOUR [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT for j in NON_FOUR : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 1 , 8 , 2 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE Permute ( arr , N ) NEW_LINE DEDENT
Construct the smallest possible Array with given Sum and XOR | Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Given sum and value of Bitwise XOR ; Function Call
def findArray ( _sum , xorr ) : NEW_LINE INDENT if ( xorr > _sum or _sum % 2 != xorr % 2 ) : NEW_LINE INDENT print ( " No ▁ Array ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT if ( _sum == xorr ) : NEW_LINE INDENT if ( _sum == 0 ) : NEW_LINE INDENT print ( " Array ▁ is ▁ empty " , " ▁ with ▁ size ▁ 0" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Array ▁ size ▁ is " , 1 ) NEW_LINE print ( " Array ▁ is " , _sum ) NEW_LINE DEDENT return NEW_LINE DEDENT mid = ( _sum - xorr ) // 2 NEW_LINE if ( xorr & mid == 1 ) : NEW_LINE INDENT print ( " Array ▁ size ▁ is " , 3 ) NEW_LINE print ( " Array ▁ is " , xorr , mid , mid ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Array ▁ size ▁ is " , 2 ) NEW_LINE print ( " Array ▁ is " , ( xorr + mid ) , mid ) NEW_LINE DEDENT DEDENT _sum = 4 NEW_LINE xorr = 2 NEW_LINE findArray ( _sum , xorr ) NEW_LINE
Smallest number greater than X which is K | Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary string ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final string ; Driver Code
def Kperiodicinteger ( X , N , K ) : NEW_LINE INDENT X = list ( X ) NEW_LINE temp = X . copy ( ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT X [ j ] = X [ i ] NEW_LINE j += K NEW_LINE DEDENT DEDENT if ( X >= temp ) : NEW_LINE INDENT return X NEW_LINE DEDENT POS = 0 NEW_LINE for i in range ( K - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( X [ i ] != '9' ) : NEW_LINE INDENT X [ i ] = str ( int ( X [ i ] ) + 1 ) NEW_LINE POS = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( POS + 1 , K ) : NEW_LINE INDENT X [ i ] = '0' NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT X [ j ] = X [ i ] NEW_LINE j += K NEW_LINE DEDENT DEDENT return X NEW_LINE DEDENT N = 4 NEW_LINE K = 2 NEW_LINE X = "1215" NEW_LINE print ( * Kperiodicinteger ( X , N , K ) , sep = ' ' ) NEW_LINE
Maximum cost path in an Undirected Graph such that no edge is visited twice in a row | Python3 program for the above approach ; To store the resulting sum of the cost ; To store largest cost leaf vertex ; DFS Traversal to find the update the maximum cost of from any node to leaf ; Mark vertex as visited ; Store vertex initial cost ; Initially assuming edge not to be traversed ; Back edge found so , edge can be part of traversal ; New vertex is found ; Bitwise AND the current check with the returned check by the previous DFS Call ; Adds parent and its children cost ; Updates total cost of parent including child nodes ; Edge is part of the cycle ; Add cost of vertex to the answer ; Updates the largest cost leaf vertex ; Function to find the maximum cost from source vertex such that no two edges is traversed twice ; DFS Call ; Print the maximum cost ; Driver Code ; Cost Array ; Given Graph ; Given Source Node ; Function Call
N = 100000 NEW_LINE canTake = 0 NEW_LINE best = 0 NEW_LINE dp = [ 0 for i in range ( N ) ] NEW_LINE vis = [ 0 for i in range ( N ) ] NEW_LINE def dfs ( g , cost , u , pre ) : NEW_LINE INDENT global canTake , best NEW_LINE vis [ u ] = True NEW_LINE dp [ u ] = cost [ u ] NEW_LINE check = 1 NEW_LINE cur = cost [ u ] NEW_LINE for x in g [ u ] : NEW_LINE INDENT if ( vis [ x ] and x != pre ) : NEW_LINE INDENT check = 0 NEW_LINE DEDENT elif ( not vis [ x ] ) : NEW_LINE INDENT check &= dfs ( g , cost , x , u ) NEW_LINE cur = max ( cur , cost [ u ] + dp [ x ] ) NEW_LINE DEDENT DEDENT dp [ u ] = cur NEW_LINE if ( not check ) : NEW_LINE INDENT canTake += cost [ u ] NEW_LINE DEDENT else : NEW_LINE INDENT best = max ( best , dp [ u ] ) NEW_LINE DEDENT return check NEW_LINE DEDENT def FindMaxCost ( g , cost , source ) : NEW_LINE INDENT dfs ( g , cost , source , - 1 ) NEW_LINE print ( canTake + best ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE m = 5 NEW_LINE cost = [ 2 , 2 , 8 , 6 , 9 ] NEW_LINE g = [ [ ] for i in range ( n ) ] NEW_LINE g [ 0 ] . append ( 1 ) NEW_LINE g [ 1 ] . append ( 0 ) NEW_LINE g [ 0 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 0 ) NEW_LINE g [ 0 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 0 ) NEW_LINE g [ 1 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 1 ) NEW_LINE g [ 1 ] . append ( 4 ) NEW_LINE g [ 4 ] . append ( 1 ) NEW_LINE source = 1 NEW_LINE FindMaxCost ( g , cost , source ) NEW_LINE DEDENT
Reverse a subarray of the given array to minimize the sum of elements at even position | Function that will give the max negative value ; Check for count greater than 0 as we require only negative solution ; Function to print the minimum sum ; Taking sum of only even index elements ; Initialize two vectors v1 , v2 ; v1 will keep account for change if 1 th index element goes to 0 ; v2 will keep account for change if 1 th index element goes to 2 ; Get the max negative value ; Driver code
def after_rev ( v ) : NEW_LINE INDENT mini = 0 NEW_LINE count = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT count += v [ i ] NEW_LINE if ( count > 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT if ( mini > count ) : NEW_LINE INDENT mini = count NEW_LINE DEDENT DEDENT return mini NEW_LINE DEDENT def print_f ( arr ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , len ( arr ) , 2 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT v1 , v2 = [ ] , [ ] NEW_LINE i = 1 NEW_LINE while i + 1 < len ( arr ) : NEW_LINE INDENT v1 . append ( arr [ i + 1 ] - arr [ i ] ) NEW_LINE i += 2 NEW_LINE DEDENT i = 1 NEW_LINE while i + 1 < len ( arr ) : NEW_LINE INDENT v2 . append ( arr [ i ] - arr [ i + 1 ] ) NEW_LINE i += 2 NEW_LINE DEDENT change = min ( after_rev ( v1 ) , after_rev ( v2 ) ) NEW_LINE if ( change < 0 ) : NEW_LINE INDENT sum += change NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 4 , 3 ] NEW_LINE print_f ( arr ) NEW_LINE DEDENT
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | Python3 program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver Code
import sys NEW_LINE from collections import defaultdict NEW_LINE def moduloEquality ( A , B , n , m ) : NEW_LINE INDENT mapA = defaultdict ( int ) NEW_LINE mapB = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapA [ A [ i ] ] += 1 NEW_LINE mapB [ B [ i ] ] += 1 NEW_LINE DEDENT possibleValues = set ( ) NEW_LINE FirstElement = B [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cur = A [ i ] NEW_LINE if cur > FirstElement : NEW_LINE INDENT possibleValues . add ( m - cur + FirstElement ) NEW_LINE DEDENT else : NEW_LINE INDENT possibleValues . add ( FirstElement - cur ) NEW_LINE DEDENT DEDENT ans = sys . maxsize NEW_LINE for it in possibleValues : NEW_LINE INDENT possible = True NEW_LINE for it2 in mapA : NEW_LINE INDENT if ( mapA [ it2 ] != mapB [ ( it2 + it ) % m ] ) : NEW_LINE INDENT possible = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( possible ) : NEW_LINE INDENT ans = min ( ans , it ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE m = 3 NEW_LINE A = [ 0 , 0 , 2 , 1 ] NEW_LINE B = [ 2 , 0 , 1 , 1 ] NEW_LINE print ( moduloEquality ( A , B , n , m ) ) NEW_LINE DEDENT
Count all indices of cyclic regular parenthesis | Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; ChecK if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Given string S ; Function call
def countCyclicShifts ( S , n ) : NEW_LINE INDENT aux = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( S [ i ] == ' ( ' ) : NEW_LINE INDENT aux [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT aux [ i ] = - 1 NEW_LINE DEDENT DEDENT mn = aux [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT aux [ i ] += aux [ i - 1 ] NEW_LINE mn = min ( mn , aux [ i ] ) NEW_LINE DEDENT if ( aux [ n - 1 ] != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( aux [ i ] == mn ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT S = " ) ( ) ( " NEW_LINE N = len ( S ) NEW_LINE print ( countCyclicShifts ( S , N ) ) NEW_LINE
Maximize Profit by trading stocks based on given rate per day | Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Given array of prices ; Function call
def maxProfit ( prices , n ) : NEW_LINE INDENT profit = 0 NEW_LINE currentDay = n - 1 NEW_LINE while ( currentDay > 0 ) : NEW_LINE INDENT day = currentDay - 1 NEW_LINE while ( day >= 0 and ( prices [ currentDay ] > prices [ day ] ) ) : NEW_LINE INDENT profit += ( prices [ currentDay ] - prices [ day ] ) NEW_LINE day -= 1 NEW_LINE DEDENT currentDay = day ; NEW_LINE DEDENT return profit ; NEW_LINE DEDENT prices = [ 2 , 3 , 5 ] NEW_LINE N = len ( prices ) NEW_LINE print ( maxProfit ( prices , N ) ) NEW_LINE
Check given string is oddly palindrome or not | Set 2 | Function to check if string formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic string is not compared ; If stack is empty then return true ; Driver code ; Given string
def isOddStringPalindrome ( str , n ) : NEW_LINE INDENT oddStringSize = n // 2 ; NEW_LINE lengthOdd = True if ( oddStringSize % 2 == 1 ) else False NEW_LINE s = [ ] NEW_LINE i = 1 NEW_LINE c = 0 NEW_LINE while ( i < n and c < oddStringSize // 2 ) : NEW_LINE INDENT s . append ( str [ i ] ) NEW_LINE i += 2 NEW_LINE c += 1 NEW_LINE DEDENT if ( lengthOdd ) : NEW_LINE INDENT i = i + 2 NEW_LINE DEDENT while ( i < n and len ( s ) > 0 ) : NEW_LINE INDENT if ( s [ len ( s ) - 1 ] == str [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT i = i + 2 NEW_LINE DEDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE s = " aeafacafae " NEW_LINE if ( isOddStringPalindrome ( s , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check given string is oddly palindrome or not | Set 2 | Functions checks if characters at odd index of the string forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given string ; Function call
def isOddStringPalindrome ( Str , n ) : NEW_LINE INDENT left , right = 0 , 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT left = 1 NEW_LINE right = n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = 1 NEW_LINE right = n - 2 NEW_LINE DEDENT while ( left < n and right >= 0 and left < right ) : NEW_LINE INDENT if ( Str [ left ] != Str [ right ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT left += 2 NEW_LINE right -= 2 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE Str = " aeafacafae " NEW_LINE if ( isOddStringPalindrome ( Str , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Remove minimum characters from string to split it into three substrings under given constraints | Python 3 program for the above approach ; Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Print the characters to be removed ; Driver Code ; Given String ; Function Call
import sys NEW_LINE def min_remove ( st ) : NEW_LINE INDENT N = len ( st ) NEW_LINE prefix_a = [ 0 ] * ( N + 1 ) NEW_LINE prefix_b = [ 0 ] * ( N + 1 ) NEW_LINE prefix_c = [ 0 ] * ( N + 1 ) NEW_LINE prefix_a [ 0 ] = 0 NEW_LINE prefix_b [ 0 ] = 0 NEW_LINE prefix_c [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( st [ i - 1 ] == ' a ' ) : NEW_LINE INDENT prefix_a [ i ] = ( prefix_a [ i - 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT prefix_a [ i ] = prefix_a [ i - 1 ] NEW_LINE DEDENT if ( st [ i - 1 ] == ' b ' ) : NEW_LINE INDENT prefix_b [ i ] = ( prefix_b [ i - 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT prefix_b [ i ] = prefix_b [ i - 1 ] NEW_LINE DEDENT if ( st [ i - 1 ] == ' c ' ) : NEW_LINE INDENT prefix_c [ i ] = ( prefix_c [ i - 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT prefix_c [ i ] = prefix_c [ i - 1 ] NEW_LINE DEDENT DEDENT maxi = - sys . maxsize - 1 ; NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT for j in range ( i , N + 1 ) : NEW_LINE INDENT maxi = max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) NEW_LINE DEDENT DEDENT print ( ( N - maxi ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " aaaabaaxccac " NEW_LINE min_remove ( st ) NEW_LINE DEDENT
Create an array of size N with sum S such that no subarray exists with sum S or S | Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Given N and sum S ; Function call
def createArray ( n , s ) : NEW_LINE INDENT if ( 2 * n <= s ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT print ( 2 , end = " ▁ " ) NEW_LINE s -= 2 NEW_LINE DEDENT print ( s ) NEW_LINE print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' - 1' ) NEW_LINE DEDENT DEDENT N = 1 NEW_LINE S = 4 NEW_LINE createArray ( N , S ) NEW_LINE
Maximize partitions such that no two substrings have any common character | Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the string ; Traverse the and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver Code
def maximum_partition ( strr ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE c = 0 NEW_LINE r = 0 NEW_LINE m = { } NEW_LINE for i in range ( len ( strr ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( strr [ i ] not in m ) : NEW_LINE INDENT m [ strr [ i ] ] = i NEW_LINE DEDENT DEDENT i = 0 NEW_LINE k = m [ strr [ i ] ] NEW_LINE for i in range ( len ( strr ) ) : NEW_LINE INDENT if ( i <= k ) : NEW_LINE INDENT c = c + 1 NEW_LINE k = max ( k , m [ strr [ i ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT r = r + 1 NEW_LINE c = 1 NEW_LINE k = max ( k , m [ strr [ i ] ] ) NEW_LINE DEDENT DEDENT if ( c != 0 ) : NEW_LINE INDENT r = r + 1 NEW_LINE DEDENT return r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " ababcbacadefegdehijhklij " NEW_LINE print ( maximum_partition ( strr ) ) NEW_LINE DEDENT
Minimize steps required to move all 1 's in a matrix to a given index | Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Given matrix ; Given position ; Function call
def findAns ( mat , x , y , n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT ans += ( abs ( x - i ) + abs ( y - j ) ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT mat = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 1 ] ] NEW_LINE x = 0 NEW_LINE y = 2 NEW_LINE print ( findAns ( mat , x , y , len ( mat ) , len ( mat [ 0 ] ) ) ) NEW_LINE
Count of all possible reverse bitonic subarrays | Function that counts all the reverse bitonic subarray in arr [ ] ; To store the count of reverse bitonic subarray ; Iterate the array and select the starting element ; Iterate for selecting the ending element for subarray ; Subarray arr [ i to j ] ; For 1 length , increment the count and continue ; For Decreasing Subarray ; Check if only Decreasing ; For Increasing Subarray ; Print the total count of subarrays ; Given array arr [ ] ; Function Call
def countReversebitonic ( arr , n ) : NEW_LINE INDENT c = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE f = 0 ; NEW_LINE if ( j == i ) : NEW_LINE INDENT c += 1 ; NEW_LINE continue ; NEW_LINE DEDENT k = i + 1 ; NEW_LINE while ( k <= j and temp > arr [ k ] ) : NEW_LINE INDENT temp = arr [ k ] ; NEW_LINE k += 1 ; NEW_LINE DEDENT if ( k > j ) : NEW_LINE INDENT c += 1 ; NEW_LINE f = 2 ; NEW_LINE DEDENT while ( k <= j and temp < arr [ k ] and f != 2 ) : NEW_LINE INDENT temp = arr [ k ] ; NEW_LINE k += 1 ; NEW_LINE DEDENT if ( k > j and f != 2 ) : NEW_LINE INDENT c += 1 ; NEW_LINE f = 0 ; NEW_LINE DEDENT DEDENT DEDENT print ( c ) NEW_LINE DEDENT arr = [ 2 , 3 , 1 , 4 ] ; NEW_LINE countReversebitonic ( arr , len ( arr ) ) ; NEW_LINE