codeparrot/xlcost-text-to-code huggingface dataset:
Check if given coins can be used to pay a value of S | Function to check if it is possible to pay a value ; Loop to add the value of coin A ; Condition to check if it is possible to pay a value of S ; Driver Code
def knowPair ( a , b , n , s , m ) : NEW_LINE INDENT i = 0 NEW_LINE rem = 0 NEW_LINE count_b = 0 NEW_LINE flag = 0 NEW_LINE while ( i <= a ) : NEW_LINE INDENT rem = s - ( n * i ) NEW_LINE count_b = rem // m NEW_LINE if ( rem % m == 0 and count_b <= b ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 2 NEW_LINE n = 3 NEW_LINE S = 4 NEW_LINE m = 2 NEW_LINE knowPair ( A , B , n , S , m ) NEW_LINE DEDENT
Sum of all numbers in the given range which are divisible by M | Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; Running a loop from A to B and check if a number is divisible by i . ; If the number is divisible , then add it to sum ; Return the sum ; Driver code ; A and B define the range M is the dividend ; Printing the result
def sumDivisibles ( A , B , M ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( A , B + 1 ) : NEW_LINE INDENT if ( i % M == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 6 NEW_LINE B = 15 NEW_LINE M = 3 NEW_LINE print ( sumDivisibles ( A , B , M ) ) NEW_LINE DEDENT
Sum of all numbers in the given range which are divisible by M | Function to find the largest number smaller than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the smallest number greater than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; To bring the smallest and largest numbers in the range [ A , B ] ; To count the number of terms in the AP ; Sum of n terms of an AP ; Driver code ; A and B define the range , M is the dividend ; Printing the result
def findSmallNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT def findLargeNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N + K - rem NEW_LINE DEDENT DEDENT def sumDivisibles ( A , B , M ) : NEW_LINE INDENT sum = 0 NEW_LINE first = findSmallNum ( A , M ) NEW_LINE last = findLargeNum ( B , M ) NEW_LINE if ( first < A ) : NEW_LINE INDENT first += M NEW_LINE DEDENT if ( last > B ) : NEW_LINE INDENT first -= M NEW_LINE DEDENT n = ( B // M ) - ( A - 1 ) // M NEW_LINE return n * ( first + last ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 6 NEW_LINE B = 15 NEW_LINE M = 3 NEW_LINE print ( sumDivisibles ( A , B , M ) ) NEW_LINE DEDENT
Count of all unique substrings with non | Function to count all unique distinct character substrings ; Hashmap to store all substrings ; Iterate over all the substrings ; Boolean array to maintain all characters encountered so far ; Variable to maintain the subtill current position ; Get the position of the character in the string ; Check if the character is encountred ; Add the current character to the substring ; Insert subin Hashmap ; Driver code
def distinctSubstring ( P , N ) : NEW_LINE INDENT S = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq = [ False ] * 26 NEW_LINE s = " " NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT pos = ord ( P [ j ] ) - ord ( ' a ' ) NEW_LINE if ( freq [ pos ] == True ) : NEW_LINE INDENT break NEW_LINE DEDENT freq [ pos ] = True NEW_LINE s += P [ j ] NEW_LINE S [ s ] = 1 NEW_LINE DEDENT DEDENT return len ( S ) NEW_LINE DEDENT S = " abba " NEW_LINE N = len ( S ) NEW_LINE print ( distinctSubstring ( S , N ) ) NEW_LINE
Sum of all subarrays of size K | Function to find the sum of all subarrays of size K ; Loop to consider every subarray of size K ; Initialize sum = 0 ; Calculate sum of all elements of current subarray ; Prsum of each subarray ; Driver Code ; Function Call
def calcSum ( arr , n , k ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , k + i ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE calcSum ( arr , n , k ) NEW_LINE
Sum of all subarrays of size K | Function to find the sum of all subarrays of size K ; Initialize sum = 0 ; Consider first subarray of size k Store the sum of elements ; Print the current sum ; Consider every subarray of size k Remove first element and add current element to the window ; Add the element which enters into the window and subtract the element which pops out from the window of the size K ; Print the sum of subarray ; Drivers Code ; Function Call
def calcSum ( arr , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT print ( sum , end = " ▁ " ) NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT sum = ( sum - arr [ i - k ] ) + arr [ i ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE calcSum ( arr , n , k ) NEW_LINE DEDENT
Number of indices pair such that element pair sum from first Array is greater than second Array | Python 3 program to find the number of indices pair such that pair sum from first Array is greater than second Array ; Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code
import bisect NEW_LINE def getPairs ( A , B , n ) : NEW_LINE INDENT D = [ 0 ] * ( n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT D [ i ] = A [ i ] - B [ i ] NEW_LINE DEDENT D . sort ( ) NEW_LINE total = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( D [ i ] > 0 ) : NEW_LINE INDENT total += n - i - 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = bisect . bisect_right ( D , - D [ i ] , 0 , len ( D ) ) NEW_LINE total += n - k NEW_LINE DEDENT DEDENT return total NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE A = [ ] NEW_LINE B = [ ] NEW_LINE A . append ( 4 ) ; NEW_LINE A . append ( 8 ) ; NEW_LINE A . append ( 2 ) ; NEW_LINE A . append ( 6 ) ; NEW_LINE A . append ( 2 ) ; NEW_LINE B . append ( 4 ) ; NEW_LINE B . append ( 5 ) ; NEW_LINE B . append ( 4 ) ; NEW_LINE B . append ( 1 ) ; NEW_LINE B . append ( 3 ) ; NEW_LINE print ( getPairs ( A , B , n ) ) NEW_LINE DEDENT
Number of pairs such that their HCF and LCM is equal | Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) / gcd ( a , b ) ; NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT
Number of pairs such that their HCF and LCM is equal | Python 3 program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
from collections import defaultdict NEW_LINE def countPairs ( a , n ) : NEW_LINE INDENT frequency = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT frequency [ a [ i ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for x in frequency . keys ( ) : NEW_LINE INDENT f = frequency [ x ] NEW_LINE count += f * ( f - 1 ) // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE DEDENT
Find the maximum possible value of last element of the Array | Function to find the maximum possible value of last element of the array ; Traverse for all element ; Find the distance ; If moves less than distance then we can not move this number to end ; How many number we can move to end ; Take the minimum of both of them ; Increment in the end ; Remove taken moves ; Return the last element ; Driver code ; Function call
def maxValue ( arr , n , moves ) : NEW_LINE INDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT distance = n - 1 - i NEW_LINE if ( moves < distance ) : NEW_LINE INDENT break NEW_LINE DEDENT can_take = moves // distance NEW_LINE take = min ( arr [ i ] , can_take ) NEW_LINE arr [ n - 1 ] += take NEW_LINE moves -= take * distance NEW_LINE DEDENT DEDENT return arr [ n - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 0 , 1 ] NEW_LINE M = 5 NEW_LINE N = len ( arr ) NEW_LINE print ( maxValue ( arr , N , M ) ) NEW_LINE DEDENT
Minimum number of Factorials whose sum is equal to N | Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Prmin count ; Prresult ; Driver program
fact = [ 0 ] * 14 NEW_LINE size = 1 NEW_LINE def preCompute ( N ) : NEW_LINE INDENT global size NEW_LINE fact [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE while fact [ i - 1 ] <= N : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE size += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def findMin ( N ) : NEW_LINE INDENT preCompute ( N ) NEW_LINE originalN = N NEW_LINE ans = [ ] NEW_LINE for i in range ( size - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( N >= fact [ i ] ) : NEW_LINE INDENT N -= fact [ i ] NEW_LINE ans . append ( fact [ i ] ) NEW_LINE DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 27 NEW_LINE findMin ( n ) NEW_LINE
Longest increasing sub | Function to find the LCS ; Loop to create frequency array ; Driver code
def findLCS ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT return len ( mp ) NEW_LINE DEDENT n = 3 NEW_LINE arr = [ 3 , 2 , 1 ] NEW_LINE print ( findLCS ( arr , n ) ) NEW_LINE
Path with maximum product in 2 | Python Program to find maximum product path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Function to find maximum product ; It will store the maximum product till a given cell . ; It will store the minimum product till a given cell ( for - ve elements ) ; If we are at topmost or leftmost , just copy the elements . ; If we 're not at the  above, we can consider  the above value. ; If we 're not on the  leftmost, we can consider  the left value. ; Store max & min product till i , j . ; Return the max product path from 0 , 0. N - 1 , M - 1. ; Driver Code ; Prmaximum product from ( 0 , 0 ) to ( N - 1 , M - 1 )
import sys NEW_LINE N = 3 ; NEW_LINE M = 3 ; NEW_LINE def maxProductPath ( arr ) : NEW_LINE INDENT maxPath = [ [ 0 for i in range ( M ) ] for j in range ( N ) ] ; NEW_LINE minPath = [ [ 0 for i in range ( M ) ] for j in range ( N ) ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT minVal = sys . maxsize ; NEW_LINE maxVal = - sys . maxsize ; NEW_LINE if ( i == 0 and j == 0 ) : NEW_LINE INDENT maxVal = arr [ i ] [ j ] ; NEW_LINE minVal = arr [ i ] [ j ] ; NEW_LINE DEDENT if ( i > 0 ) : NEW_LINE INDENT tempMax = max ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , \ minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; NEW_LINE maxVal = max ( maxVal , tempMax ) ; NEW_LINE tempMin = min ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , \ minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; NEW_LINE minVal = min ( minVal , tempMin ) ; NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT tempMax = max ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , \ minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; NEW_LINE maxVal = max ( maxVal , tempMax ) ; NEW_LINE tempMin = min ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , \ minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; NEW_LINE minVal = min ( minVal , tempMin ) ; NEW_LINE DEDENT maxPath [ i ] [ j ] = maxVal ; NEW_LINE minPath [ i ] [ j ] = minVal ; NEW_LINE DEDENT DEDENT return maxPath [ N - 1 ] [ M - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , - 2 , 3 ] , [ 4 , - 5 , 6 ] , [ - 7 , - 8 , 9 ] ] ; NEW_LINE print ( maxProductPath ( arr ) ) ; NEW_LINE DEDENT
Vertical and Horizontal retrieval ( MRT ) on Tapes | Python3 program to print Vertical filling ; 2D matrix for vertical insertion on tapes ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; It is used for calculating mean retrieval time ; vertical insertion on tape ; initialize variables to 0 for each iteration ; Used for getting ' sum ' sizes of records in tape to determine whether tape is full or not ; if tape is not full ; check for ability of tapes to hold value ; initialize variables to 0 for each iteration ; display elements of tape ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tape [ ] ; sorting of an array is required to attain greedy approach of algorithm
import numpy as np NEW_LINE def vertical_Fill ( records , tape , m , n ) : NEW_LINE INDENT v = np . zeros ( ( m , n ) ) ; NEW_LINE sum = 0 ; NEW_LINE Retrieval_Time = 0 ; NEW_LINE Mrt = None ; NEW_LINE z = 0 ; j = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for k in range ( i ) : NEW_LINE INDENT sum += v [ j ] [ k ] ; NEW_LINE DEDENT if ( sum + records [ z ] <= tape [ j ] ) : NEW_LINE INDENT v [ j ] [ i ] = records [ z ] ; NEW_LINE z += 1 ; NEW_LINE DEDENT DEDENT if ( v [ 2 ] [ i ] == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT Retrieval_Time = 0 ; NEW_LINE print ( " tape " , i + 1 , " : ▁ [ " , end = " " ) ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( v [ i ] [ j ] != 0 ) : NEW_LINE INDENT print ( v [ i ] [ j ] , end = " ▁ " ) ; else : break ; print ( " ] " , end = " " ) ; NEW_LINE DEDENT DEDENT k = 0 ; NEW_LINE while v [ i ] [ k ] != 0 : NEW_LINE INDENT Retrieval_Time += v [ i ] [ k ] * ( j - k ) ; NEW_LINE k += 1 ; NEW_LINE DEDENT Mrt = Retrieval_Time / j ; NEW_LINE print ( " MRT ▁ : " , Mrt ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ] ; NEW_LINE tape = [ 25 , 80 , 160 ] ; NEW_LINE n = len ( records ) ; NEW_LINE m = len ( tape ) ; NEW_LINE records . sort ( ) ; NEW_LINE vertical_Fill ( records , tape , m , n ) ; NEW_LINE DEDENT
Minimum number of subsequences required to convert one string to another | Python3 program to find the Minimum number of subsequences required to convert one to another ; Function to find the no of subsequences ; Push the values of indexes of each character ; Find the next index available in the array ; If Character is not in A ; Check if the next index is not equal to the size of array which means there is no index greater than minIndex in the array ; Update value of minIndex with this index ; Update the value of counter and minIndex for next operation ; Driver Code
from bisect import bisect as upper_bound NEW_LINE def minSubsequnces ( A , B ) : NEW_LINE INDENT v = [ [ ] for i in range ( 26 ) ] NEW_LINE minIndex = - 1 NEW_LINE cnt = 1 NEW_LINE j = 0 NEW_LINE flag = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT p = ord ( A [ i ] ) - 97 NEW_LINE v [ p ] . append ( i ) NEW_LINE DEDENT while ( j < len ( B ) ) : NEW_LINE INDENT p = ord ( B [ j ] ) - 97 NEW_LINE k = upper_bound ( v [ p ] , minIndex ) NEW_LINE if ( len ( v [ p ] ) == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT if ( k != len ( v [ p ] ) ) : NEW_LINE INDENT minIndex = v [ p ] [ k ] NEW_LINE j = j + 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE minIndex = - 1 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT A1 = " abbace " NEW_LINE B1 = " acebbaae " NEW_LINE print ( minSubsequnces ( A1 , B1 ) ) NEW_LINE
Find two equal subsequences of maximum length with at least one different index | Python implementation of the approach ; Function to return the required length of the subsequences ; To store the result ; To store the last visited position of lowercase letters ; Initialisation of frequency array to - 1 to indicate no character has previously occured ; For every character of the String ; Get the index of the current character ; If the current character has appeared before in the String ; Update the result ; Update the last position of the current character ; Driver code
MAX = 26 ; NEW_LINE def maxLength ( str , len ) : NEW_LINE INDENT res = 0 ; NEW_LINE lastPos = [ 0 ] * MAX ; NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT lastPos [ i ] = - 1 ; NEW_LINE DEDENT for i in range ( len ) : NEW_LINE INDENT C = ord ( str [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( lastPos [ C ] != - 1 ) : NEW_LINE INDENT res = max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; NEW_LINE DEDENT lastPos [ C ] = i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE len = len ( str ) ; NEW_LINE print ( maxLength ( str , len ) ) ; NEW_LINE DEDENT
Maximum profit by selling N items at two markets | Python3 implementation of the approach ; Max profit will be saved here ; loop to check all possible combinations of sales ; the sum of the profit after the sale for products 0 to i in market A ; the sum of the profit after the sale for products i to n in market B ; Replace the value of Max Profit with a bigger value among maxP and sumA + sumB ; Return the value of Max Profit ; Driver Program
def maxProfit ( a , b , n ) : NEW_LINE INDENT maxP = - 1 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT sumA = sum ( a [ : i ] ) NEW_LINE sumB = sum ( b [ i : ] ) NEW_LINE maxP = max ( maxP , sumA + sumB ) NEW_LINE DEDENT return maxP NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 3 , 2 ] NEW_LINE b = [ 10 , 30 , 40 ] NEW_LINE print ( maxProfit ( a , b , 4 ) ) NEW_LINE DEDENT
Partitions possible such that the minimum element divides all the other elements of the partition | Python3 implementation of the approach ; Function to return the count partitions possible from the given array such that the minimum element of any partition divides all the other elements of that partition ; Initialize the count variable ; Find the minimum element ; Break if no minimum element present ; Increment the count if minimum element present ; Replace all the element divisible by min_elem ; Driver code
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def countPartitions ( A , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT min_elem = min ( A ) ; NEW_LINE if ( min_elem == INT_MAX ) : NEW_LINE INDENT break ; NEW_LINE DEDENT count += 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % min_elem == 0 ) : NEW_LINE INDENT A [ i ] = INT_MAX ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( countPartitions ( arr , N ) ) ; NEW_LINE DEDENT
Minimum number of swaps to make two binary string equal | Function to calculate min swaps to make binary strings equal ; Count of zero 's  ; Count of one 's  ; As discussed above ; Driver code
def minSwaps ( s1 , s2 ) : NEW_LINE INDENT c0 = 0 ; c1 = 0 ; NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == '0' and s2 [ i ] == '1' ) : NEW_LINE INDENT c0 += 1 ; NEW_LINE DEDENT elif ( s1 [ i ] == '1' and s2 [ i ] == '0' ) : NEW_LINE INDENT c1 += 1 ; NEW_LINE DEDENT DEDENT ans = c0 // 2 + c1 // 2 ; NEW_LINE if ( c0 % 2 == 0 and c1 % 2 == 0 ) : NEW_LINE INDENT return ans ; NEW_LINE DEDENT elif ( ( c0 + c1 ) % 2 == 0 ) : NEW_LINE INDENT return ans + 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "0011" ; s2 = "1111" ; NEW_LINE ans = minSwaps ( s1 , s2 ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Minimum changes required to make all element in an array equal | Function to count of minimum changes required to make all elements equal ; Store the count of each element as key - value pair in Dictionary ; Traverse through the Dictionary and find the maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
def minChanges ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT maxElem = 0 NEW_LINE for x in mp : NEW_LINE INDENT maxElem = max ( maxElem , mp [ x ] ) NEW_LINE DEDENT return n - maxElem NEW_LINE DEDENT arr = [ 2 , 3 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minChanges ( arr , n ) ) NEW_LINE