codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of Apples to be collected from trees to guarantee M red apples | Function to minimum no . of apples ; If we get all required apple from South ; If we required trees at East and West ; If we doesn 't have enough  red apples ; Driver Code ; No . of red apple for gift ; No . of red apple in each tree ; No . of tree in North ; No . of tree in South ; No . of tree in West ; No . of tree in East ; Function Call
def minApples ( ) : NEW_LINE INDENT if M <= S * K : NEW_LINE INDENT return M NEW_LINE DEDENT elif M <= S * K + E + W : NEW_LINE INDENT return S * K + ( M - S * K ) * K NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = 10 NEW_LINE K = 15 NEW_LINE N = 0 NEW_LINE S = 1 NEW_LINE W = 0 NEW_LINE E = 0 NEW_LINE ans = minApples ( ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Minimum increments or decrements required to signs of prefix sum array elements alternating | Function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; Case 1. neg - pos - neg ; Stores the current sign of the prefix sum of array ; Stores minimum number of operations for Case 1 ; Traverse the array ; Checking both conditions ; Update the current prefix1 to currentPrefixSum ; Case 2. pos - neg - pos ; Stores the prefix sum of array ; Stores minimum number of operations for Case 1 ; Checking both conditions ; Update the current prefix2 to currentPrefixSum ; Driver Code
def minimumOperations ( A , N ) : NEW_LINE INDENT cur_prefix_1 = 0 NEW_LINE parity = - 1 NEW_LINE minOperationsCase1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cur_prefix_1 += A [ i ] NEW_LINE if ( cur_prefix_1 == 0 or parity * cur_prefix_1 < 0 ) : NEW_LINE INDENT minOperationsCase1 += abs ( parity - cur_prefix_1 ) NEW_LINE cur_prefix_1 = parity NEW_LINE DEDENT parity *= - 1 NEW_LINE DEDENT cur_prefix_2 = 0 NEW_LINE parity = 1 NEW_LINE minOperationsCase2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cur_prefix_2 += A [ i ] NEW_LINE if ( cur_prefix_2 == 0 or parity * cur_prefix_2 < 0 ) : NEW_LINE INDENT minOperationsCase2 += abs ( parity - cur_prefix_2 ) NEW_LINE cur_prefix_2 = parity NEW_LINE DEDENT parity *= - 1 NEW_LINE DEDENT return min ( minOperationsCase1 , minOperationsCase2 ) NEW_LINE DEDENT A = [ 1 , - 3 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumOperations ( A , N ) ) NEW_LINE
Generate an array of maximum sum such that each element exceeds all elements present either on its left or right | Function to construct the array having maximum sum satisfying the given criteria ; Declaration of the array arrA [ ] and ans [ ] ; Stores the maximum sum of the resultant array ; Initialize the array arrA [ ] ; Traversing the array arrA [ ] ; Initialize the array arrB [ ] ; Assign the maximum element to the current element ; Form the first increasing till every index i ; Make the current element as the maximum element ; Forming decreasing from the index i + 1 to the index N ; Initialize the sum ; Find the total sum ; Check if the total sum is at least the sum found then make ans as ansB ; Print the final array formed ; Driver Code
def maximumSumArray ( arr , N ) : NEW_LINE INDENT arrA = [ 0 ] * N NEW_LINE ans = [ 0 ] * N NEW_LINE maxSum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT arrA [ i ] = arr [ i ] ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT arrB = [ 0 ] * N NEW_LINE maximum = arrA [ i ] ; NEW_LINE arrB [ i ] = maximum ; NEW_LINE temp = 0 NEW_LINE for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT arrB [ j ] = min ( maximum , arrA [ j ] ) ; NEW_LINE maximum = arrB [ j ] ; NEW_LINE temp = j NEW_LINE DEDENT maximum = arrA [ i ] ; NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT arrB [ j ] = min ( maximum , arrA [ j ] ) ; NEW_LINE maximum = arrB [ j ] ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for j in range ( N ) : NEW_LINE INDENT sum += arrB [ j ] ; NEW_LINE DEDENT if ( sum > maxSum ) : NEW_LINE INDENT maxSum = sum ; NEW_LINE ans = arrB ; NEW_LINE DEDENT DEDENT for val in ans : NEW_LINE INDENT print ( val ) ; NEW_LINE DEDENT DEDENT A = [ 10 , 6 , 8 ] ; NEW_LINE N = len ( A ) NEW_LINE maximumSumArray ( A , N ) ; NEW_LINE
Minimize product of two scores possible by at most M reductions | Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; case 2 - R2 reduces first ; Driver Code ; Given Input ; Function Call
def minProductUtil ( R1 , B1 , R2 , B2 , M ) : NEW_LINE INDENT x = min ( R1 - B1 , M ) NEW_LINE M -= x NEW_LINE R1 -= x NEW_LINE if M > 0 : NEW_LINE INDENT y = min ( R2 - B2 , M ) NEW_LINE M -= y NEW_LINE R2 -= y NEW_LINE DEDENT return R1 * R2 NEW_LINE DEDENT def minProduct ( R1 , B1 , R2 , B2 , M ) : NEW_LINE INDENT res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) NEW_LINE res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) NEW_LINE return min ( res1 , res2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R1 = 21 ; B1 = 10 ; R2 = 13 ; B2 = 11 ; M = 3 NEW_LINE print ( minProduct ( R1 , B1 , R2 , B2 , M ) ) NEW_LINE DEDENT
Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | Function to find maximum profit after selling K tickets ; Frequency array to store freq of every element of the array ; Modify the arr [ ] so that the array is sorted in O ( N ) ; Variable to store answer ; Traverse the array while K > 0 and j >= 0 ; If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ] ; If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; Sell tickets from counter [ j + 1 , N ] ; All elements of array are equal Send tickets from each counter 1 time until K > 0. ; Converting answer from long long to int ; Driver Code ; Given Input ; Function Call
def maxAmount ( n , k , arr ) : NEW_LINE INDENT A = [ 0 for i in range ( 1000001 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT A [ arr [ i ] ] += 1 NEW_LINE DEDENT j = 0 NEW_LINE for j in range ( 1000001 ) : NEW_LINE INDENT while ( A [ i ] != 0 ) : NEW_LINE INDENT arr [ j ] = i ; NEW_LINE j += 1 NEW_LINE A [ i ] -= 1 NEW_LINE DEDENT DEDENT ans = 6 NEW_LINE mod = 1000000007 NEW_LINE i = n - 1 NEW_LINE j = n - 2 NEW_LINE while ( k > 0 and j >= 0 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT ans = ans + min ( k , ( i - j ) ) * arr [ i ] NEW_LINE k = k - ( i - j ) NEW_LINE arr [ i ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT while ( j >= 0 and arr [ j ] == arr [ i ] ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT if ( j < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = ans + min ( k , ( i - j ) ) * arr [ i ] NEW_LINE k = k - ( i - j ) NEW_LINE arr [ i ] -= 1 NEW_LINE DEDENT DEDENT while ( k > 0 and arr [ i ] != 0 ) : NEW_LINE INDENT ans = ans + min ( n , k ) * arr [ i ] NEW_LINE k -= n NEW_LINE arr [ i ] -= 1 NEW_LINE DEDENT ans = ans % mod NEW_LINE x = ans NEW_LINE return x NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE k = 3 NEW_LINE arr = [ 4 , 3 , 6 , 2 , 4 ] NEW_LINE ans = maxAmount ( n , k , arr ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Maximize sum of averages of subsequences of lengths lying in a given range | Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code
def maxAverage ( A , N , X , Y ) : NEW_LINE INDENT A . sort ( ) NEW_LINE sum = 0 NEW_LINE res = 0 NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE count += 1 NEW_LINE if ( count == X ) : NEW_LINE INDENT if ( N - i - 1 < X ) : NEW_LINE INDENT i += 1 NEW_LINE cnt = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT cnt += 1 NEW_LINE sum += A [ i ] NEW_LINE i += 1 NEW_LINE DEDENT X = X + cnt NEW_LINE res += sum / X NEW_LINE break NEW_LINE DEDENT res += sum / X NEW_LINE sum = 0 NEW_LINE count = 0 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 10 , 6 , 5 ] NEW_LINE N = len ( A ) NEW_LINE X = 2 NEW_LINE Y = 3 NEW_LINE maxAverage ( A , N , X , Y ) NEW_LINE DEDENT
Rearrange characters in a sorted string such that no pair of adjacent characters are the same | Python 3 program for the above approach ; Function to check if a string S contains pair of adjacent characters that are equal or not ; Traverse the string S ; If S [ i ] and S [ i + 1 ] are equal ; Otherwise , return false ; Function to rearrange characters of a string such that no pair of adjacent characters are the same ; Initialize 3 variables ; Iterate until k < N ; If S [ i ] is not equal to S [ j ] ; Increment i and j by 1 ; If j equals k and increment the value of K by 1 ; Else ; If S [ j ] equals S [ k ] ; Increment k by 1 ; Else ; Swap ; Increment i and j by 1 ; If j equals k ; Increment k by 1 ; Function to rearrange characters in a string so that no two adjacent characters are same ; If string is already valid ; If size of the string is 2 ; Function Call ; Reversing the string ; Function Call ; If the string is valid ; Otherwise ; Driver Code
S = " aaabc " NEW_LINE def isAdjChar ( s ) : NEW_LINE INDENT for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def rearrangeStringUtil ( N ) : NEW_LINE INDENT global S NEW_LINE S = list ( S ) NEW_LINE i = 0 NEW_LINE j = 1 NEW_LINE k = 2 NEW_LINE while ( k < N ) : NEW_LINE INDENT if ( S [ i ] != S [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE if ( j == k ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( S [ j ] == S [ k ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp = S [ k ] NEW_LINE S [ k ] = S [ j ] NEW_LINE S [ j ] = temp NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE if ( j == k ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT S = ' ' . join ( S ) NEW_LINE DEDENT def rearrangeString ( N ) : NEW_LINE INDENT global S NEW_LINE if ( isAdjChar ( S ) == False ) : NEW_LINE INDENT return S NEW_LINE DEDENT if ( len ( S ) == 2 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT rearrangeStringUtil ( N ) NEW_LINE S = S [ : : - 1 ] NEW_LINE rearrangeStringUtil ( N ) NEW_LINE if ( isAdjChar ( S ) == False ) : NEW_LINE INDENT return S NEW_LINE DEDENT return " - 1" NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = len ( S ) NEW_LINE print ( rearrangeString ( N ) ) NEW_LINE DEDENT
Lexicographically largest string possible by repeatedly appending first character of two given strings | Function to make the lexicographically largest string by merging two strings ; Stores the resultant string ; If the string word1 is lexographically greater than or equal to word2 ; Update the string merge ; Erase the first index of the string word1 ; Otherwise ; Update the string merge ; Erase the first index of the string word2 ; Return the final string ; Driver code
def largestMerge ( word1 , word2 ) : NEW_LINE INDENT merge = " " NEW_LINE while len ( word1 ) != 0 or len ( word2 ) != 0 : NEW_LINE INDENT if word1 >= word2 : NEW_LINE INDENT merge = merge + word1 [ 0 ] NEW_LINE word1 = word1 [ 1 : ] NEW_LINE DEDENT else : NEW_LINE INDENT merge = merge + word2 [ 0 ] NEW_LINE word2 = word2 [ 1 : ] NEW_LINE DEDENT DEDENT return merge NEW_LINE DEDENT S1 = " xyzxyz " NEW_LINE S2 = " xywzxyx " NEW_LINE print ( largestMerge ( S1 , S2 ) ) NEW_LINE
Maximum rods to put horizontally such that no two rods overlap on X coordinate | Python 3 program for the above approach ; Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver Code
import sys NEW_LINE def findMaximumPoints ( N , X , H ) : NEW_LINE INDENT ans = 0 NEW_LINE prev = - sys . maxsize - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( prev < ( X [ i ] - H [ i ] ) ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = X [ i ] NEW_LINE DEDENT elif ( i == N - 1 or ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = X [ i ] + H [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT prev = X [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 1 , 2 , 3 ] NEW_LINE H = [ 2 , 5 , 5 ] NEW_LINE N = len ( X ) NEW_LINE print ( findMaximumPoints ( N , X , H ) ) NEW_LINE DEDENT
Maximize the missing values in given time in HH : MM format | Function to find the maximum time by replacing ' ? ' by any digits ; If the 0 th index is '? ; If the 1 st index is '? ; If the 3 rd index is '? ; If the 4 th index is '? ; Return new string ; Driver Code
def maxTime ( s ) : NEW_LINE ' NEW_LINE INDENT s = list ( s ) NEW_LINE if ( s [ 0 ] == ' ? ' ) : NEW_LINE INDENT if ( s [ 1 ] <= '3' or s [ 1 ] == ' ? ' ) : NEW_LINE INDENT s [ 0 ] = '2' NEW_LINE DEDENT else : NEW_LINE INDENT s [ 0 ] = '1' NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 1 ] == ' ? ' ) : NEW_LINE INDENT if ( s [ 0 ] != '2' ) : NEW_LINE INDENT s [ 1 ] = 9 NEW_LINE DEDENT else : NEW_LINE INDENT s [ 1 ] = 3 NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 3 ] == ' ? ' ) : NEW_LINE INDENT s [ 3 ] = '5' NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 4 ] == ' ? ' ) : NEW_LINE INDENT s [ 4 ] = '9' NEW_LINE DEDENT print ( " " . join ( s ) ) NEW_LINE DEDENT S = " ? 4:5 ? " NEW_LINE maxTime ( S ) NEW_LINE
Maximum GCD of two numbers possible by adding same value to them | Function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; Given Input
def maxGcd ( a , b ) : NEW_LINE INDENT print ( abs ( a - b ) ) NEW_LINE DEDENT a = 2231 NEW_LINE b = 343 NEW_LINE maxGcd ( a , b ) NEW_LINE
Count the combination of 4 s and / or 5 s required to make each Array element 0 | Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Given Input ; Function Call
def sumOfCombinationOf4OR5 ( arr , N ) : NEW_LINE INDENT ans = [ - 1 for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 4 ) : NEW_LINE INDENT continue NEW_LINE DEDENT sum = 10 ** 9 NEW_LINE cnt = 0 NEW_LINE for j in range ( 0 , arr [ i ] + 1 , 4 ) : NEW_LINE INDENT if ( ( arr [ i ] - j ) % 5 == 0 ) : NEW_LINE INDENT sum = min ( sum , cnt + ( arr [ i ] - j ) // 5 ) NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT if ( sum != 10 ** 9 ) : NEW_LINE INDENT ans [ i ] = sum NEW_LINE DEDENT DEDENT for num in ans : NEW_LINE INDENT print ( num , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 7 , 15 , 17 , 22 ] NEW_LINE N = len ( arr ) NEW_LINE sumOfCombinationOf4OR5 ( arr , N ) NEW_LINE
Find an N | Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call
def printBinaryString ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT print ( 0 , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( 1 , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT N = 5 ; NEW_LINE M = 3 ; NEW_LINE arr = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] ] ; NEW_LINE printBinaryString ( arr , N ) ; NEW_LINE
Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | Python program for the above approach ; Function to find the maximum number of 0 s in the array A [ ] after changing the array element to A [ i ] * D + B [ i ] ; Stores the frequency of fractions needed to make each element 0 ; Stores the maximum number of 0 ; Traverse the array ; Find the numerator and the denominator ; Check if den is not equal to 0 ; Divide num and den by their gcd ; Check if num is not greater than 0 ; Check if both num and den are equal to 0 ; Increment the value of { num , den } in the map ; Update the value of ans ; Prthe value of ans + cnt ; Driver Code
from math import gcd NEW_LINE def maxZeroes ( A , B ) : NEW_LINE INDENT mp = { } NEW_LINE N = len ( A ) NEW_LINE ans = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT num = - B [ i ] NEW_LINE den = A [ i ] NEW_LINE gc = gcd ( num , den ) NEW_LINE if ( den != 0 ) : NEW_LINE INDENT num //= gc NEW_LINE den //= gc NEW_LINE DEDENT if ( num <= 0 ) : NEW_LINE INDENT num *= - 1 NEW_LINE den *= - 1 NEW_LINE DEDENT if ( den == 0 and num == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( den != 0 ) : NEW_LINE INDENT mp [ ( num , den ) ] = mp . get ( ( num , den ) , 0 ) + 1 NEW_LINE ans = max ( mp [ ( num , den ) ] , ans ) NEW_LINE DEDENT DEDENT print ( ans + cnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , - 1 ] NEW_LINE B = [ - 6 , - 12 , 6 ] NEW_LINE maxZeroes ( A , B ) NEW_LINE DEDENT
Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Poleter of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code
def MinCost ( days , cost , N ) : NEW_LINE INDENT size = days [ N - 1 ] + 1 NEW_LINE dp = [ 0 for i in range ( size ) ] NEW_LINE dp [ size - 1 ] = min ( cost [ 0 ] , min ( cost [ 1 ] , cost [ 2 ] ) ) NEW_LINE ptr = N - 2 NEW_LINE for i in range ( size - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( ptr >= 0 and days [ ptr ] == i ) : NEW_LINE INDENT val1 = dp [ i + 1 ] + cost [ 0 ] NEW_LINE val2 = cost [ 1 ] + ( 0 if ( i + 7 >= size ) else dp [ i + 7 ] ) NEW_LINE val3 = cost [ 2 ] + ( 0 if ( i + 30 >= size ) else dp [ i + 30 ] ) NEW_LINE dp [ i ] = min ( val1 , min ( val2 , val3 ) ) NEW_LINE ptr -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i + 1 ] NEW_LINE DEDENT DEDENT return dp [ 1 ] NEW_LINE DEDENT arr = [ 2 , 4 , 6 , 7 , 8 , 10 , 17 ] NEW_LINE cost = [ 3 , 8 , 20 ] NEW_LINE N = len ( arr ) NEW_LINE print ( MinCost ( arr , cost , N ) ) NEW_LINE
Maximum Pairs of Bracket Sequences which can be concatenated to form a Regular Bracket Sequence | Function to count the number of pairs whose concatenation results in the regular bracket sequence ; Stores the count of opening and closing parenthesis for each string arr [ i ] ; Stores maximum count of pairs ; Traverse the array arr [ ] ; Traverse the string c [ ] ; Opening Bracket ; Otherwise , Closing Bracket ; Count of closing brackets needed to balance string ; Count of opening brackets needed to balance string ; Add the count of balanced sequences ; Traverse the array ; Print the resultant count ; Driver Code
def countPairs ( N , arr ) : NEW_LINE INDENT open = [ 0 ] * 100 NEW_LINE close = [ 0 ] * 100 NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT c = [ i for i in arr [ i ] ] NEW_LINE d = 0 NEW_LINE minm = 0 NEW_LINE for j in range ( len ( c ) ) : NEW_LINE INDENT if ( c [ j ] == ' ( ' ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT else : NEW_LINE INDENT d -= 1 NEW_LINE if ( d < minm ) : NEW_LINE INDENT minm = d NEW_LINE DEDENT DEDENT DEDENT if ( d >= 0 ) : NEW_LINE INDENT if ( minm == 0 ) : NEW_LINE INDENT close [ d ] += 1 NEW_LINE DEDENT DEDENT elif ( d < 0 ) : NEW_LINE INDENT if ( minm == d ) : NEW_LINE INDENT open [ - d ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans += close [ 0 ] // 2 NEW_LINE for i in range ( 1 , 100 ) : NEW_LINE INDENT ans += min ( open [ i ] , close [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE list = [ ] NEW_LINE list . append ( " ) ( ) ) " ) NEW_LINE list . append ( " ) " ) NEW_LINE list . append ( " ( ( " ) NEW_LINE list . append ( " ( ( " ) NEW_LINE list . append ( " ( " ) NEW_LINE list . append ( " ) " ) NEW_LINE list . append ( " ) " ) NEW_LINE countPairs ( N , list ) NEW_LINE DEDENT
Minimum count of elements to be inserted in Array to form all values in [ 1 , K ] using subset sum | Function to find the count of minimum elements to be inserted to form every number in a range ; Stores the count of numbers needed ; Stores the numbers upto which every numbers can be formed ; Stores the index of the array arr [ ] ; Iterate until requiredSum is less than or equal to K ; If i is less than N and requiredSum is greater than or equal to arr [ i ] ; Increment requiredSum by arr [ i ] ; Increment i by 1 ; Otherwise ; Increment count by 1 ; Increment requiredSum by requiredSum ; Return result ; Driver Code ; Input ; Function Call
def minElements ( arr , N , K ) : NEW_LINE INDENT count = 0 NEW_LINE requiredNum = 1 NEW_LINE i = 0 NEW_LINE while ( requiredNum <= K ) : NEW_LINE INDENT if ( i < N and requiredNum >= arr [ i ] ) : NEW_LINE INDENT requiredNum += arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE requiredNum += requiredNum NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 ] NEW_LINE K = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( minElements ( arr , N , K ) ) NEW_LINE DEDENT
Theft at World Bank | Python3 program for the above approach ; Custom comparator ; Function to find the maximum profit ; Stores the pairs of elements of B and A at the same index ; Iterate over the range [ 0 , N ] ; If current integer is perfect square ; Push the pair of B [ i ] and A [ i ] in vector V ; Sort the vector using the custom comparator ; Stores the maximum profit ; Traverse the vector V ; If V [ i ] [ 1 ] is less than W ; Increment profit by V [ i ] [ 0 ] ; Decrement V [ i ] [ 0 ] from W ; Otherwise ; Update profit ; Return the value of profit ; Driver Code
import math NEW_LINE from functools import cmp_to_key NEW_LINE def comparator ( p1 , p2 ) : NEW_LINE INDENT a = p1 [ 0 ] NEW_LINE b = p1 [ 1 ] NEW_LINE c = p2 [ 0 ] NEW_LINE d = p2 [ 1 ] NEW_LINE val1 = a / b NEW_LINE val2 = c / d NEW_LINE return val1 > val2 NEW_LINE DEDENT def maximumProfit ( A , B , N , W ) : NEW_LINE INDENT V = [ ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT temp = int ( math . sqrt ( A [ i ] ) ) NEW_LINE if temp * temp == A [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT V . append ( [ B [ i ] , A [ i ] ] ) NEW_LINE DEDENT V = sorted ( V , key = cmp_to_key ( comparator ) ) NEW_LINE profit = 0.00 NEW_LINE k = len ( V ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if V [ i ] [ 1 ] <= W : NEW_LINE INDENT profit += V [ i ] [ 0 ] NEW_LINE W -= V [ i ] [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT profit += ( V [ i ] [ 0 ] * W ) / V [ i ] [ 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT return profit NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE W = 10 NEW_LINE A = [ 4 , 5 , 7 ] NEW_LINE B = [ 8 , 5 , 4 ] NEW_LINE print ( round ( maximumProfit ( A , B , N , W ) , 5 ) ) NEW_LINE DEDENT