codeparrot/xlcost-text-to-code huggingface dataset:
Sum of product of consecutive Binomial Coefficients | Python3 Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driver Code
MAX = 100 ; NEW_LINE def binomialCoeff ( C , n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , n ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] ; NEW_LINE DEDENT DEDENT return C ; NEW_LINE DEDENT def sumOfproduct ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE C = [ 0 ] * MAX ; NEW_LINE C = binomialCoeff ( C , n ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT sum += C [ i ] * C [ i + 1 ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( sumOfproduct ( n ) ) ; NEW_LINE
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Python 3 Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; C [ 0 ] = 1 nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driver Code
MAX = 100 NEW_LINE def binomialCoeff ( n , C ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , n ) , - 1 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT def summation ( n ) : NEW_LINE INDENT C = [ 0 ] * MAX NEW_LINE binomialCoeff ( n , C ) NEW_LINE sum = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT sum += ( i * C [ i ] ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE print ( summation ( n ) ) NEW_LINE DEDENT
Number of arrays of size N whose elements are positive integers and sum is K | Return nCr ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ 0 ] * ( k + 1 ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] ; NEW_LINE DEDENT DEDENT return C [ k ] ; NEW_LINE DEDENT def countArray ( N , K ) : NEW_LINE INDENT return binomialCoeff ( K - 1 , N - 1 ) ; NEW_LINE DEDENT N = 2 ; NEW_LINE K = 3 ; NEW_LINE print ( countArray ( N , K ) ) ; NEW_LINE
Partitioning into two contiguous element subarrays with equal sums | Class to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in class . ; Driver Code
class Data : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . element = - 1 NEW_LINE self . position = - 1 NEW_LINE DEDENT DEDENT def findMinElement ( arr , n ) : NEW_LINE INDENT result = Data ( ) NEW_LINE prefixSum = [ 0 ] * n NEW_LINE suffixSum = [ 0 ] * n NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] suffixSum [ n - 1 ] = arr [ n - 1 ] NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT mini = suffixSum [ 0 ] NEW_LINE pos = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < mini : NEW_LINE INDENT mini = abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) NEW_LINE if suffixSum [ i + 1 ] < prefixSum [ i ] : NEW_LINE INDENT pos = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT pos = i NEW_LINE DEDENT DEDENT DEDENT result . element = mini NEW_LINE result . position = pos NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE values = Data ( ) NEW_LINE values = findMinElement ( arr , n ) NEW_LINE print ( " Minimum ▁ element ▁ : " , values . element ,   " Position : " , values . position ) NEW_LINE DEDENT
Number of circular tours that visit all petrol pumps | Python 3 Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Driver Code
N = 100 NEW_LINE def count ( n , c , a , b ) : NEW_LINE INDENT need = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT a [ i + n ] = a [ i ] NEW_LINE b [ i + n ] = b [ i ] NEW_LINE DEDENT s = 0 NEW_LINE tank = 0 NEW_LINE for i in range ( 0 , 2 * n , 1 ) : NEW_LINE INDENT tank += a [ i ] NEW_LINE tank = min ( tank , c ) NEW_LINE tank -= b [ i ] NEW_LINE if ( tank < 0 ) : NEW_LINE INDENT tank = 0 NEW_LINE s = i + 1 NEW_LINE DEDENT DEDENT if ( s >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 1 NEW_LINE need [ s + n ] = 0 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT id = s + n - i NEW_LINE need [ id ] = max ( 0 , need [ id + 1 ] + b [ id ] - min ( a [ id ] , c ) ) NEW_LINE if ( need [ id ] == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE c = 3 NEW_LINE a = [ 3 , 1 , 2 , 0 , 0 , 0 ] NEW_LINE b = [ 2 , 2 , 2 , 0 , 0 , 0 ] NEW_LINE print ( count ( n , c , a , b ) ) NEW_LINE DEDENT
Print equal sum sets of array ( Partition Problem ) | Set 2 | Python3 program to print equal sum sets of array . ; Function to print equal sum sets of array . ; Finding sum of array elements ; Check sum is even or odd . If odd then array cannot be partitioned . Print - 1 and return . ; Divide sum by 2 to find sum of two possible subsets . ; Boolean DP table to store result of states . dp [ i ] [ j ] = true if there is a subset of elements in first i elements of array that has sum equal to j . ; If number of elements are zero , then no sum can be obtained . ; Sum 0 can be obtained by not selecting any element . ; Fill the DP table in bottom up manner . ; Excluding current element . ; Including current element ; Required sets set1 and set2 . ; If partition is not possible print - 1 and return . ; Start from last element in dp table . ; If current element does not contribute to k , then it belongs to set 2. ; If current element contribute to k then it belongs to set 1. ; Print elements of both the sets . ; Driver Code
import numpy as np NEW_LINE def printEqualSumSets ( arr , n ) : NEW_LINE INDENT sum_array = sum ( arr ) NEW_LINE if ( sum_array & 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT k = sum_array >> 1 NEW_LINE dp = np . zeros ( ( n + 1 , k + 1 ) ) NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = False NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for currSum in range ( 1 , k + 1 ) : NEW_LINE INDENT dp [ i ] [ currSum ] = dp [ i - 1 ] [ currSum ] NEW_LINE if ( arr [ i - 1 ] <= currSum ) : NEW_LINE INDENT dp [ i ] [ currSum ] = ( dp [ i ] [ currSum ] or dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT set1 , set2 = [ ] , [ ] NEW_LINE if ( not dp [ n ] [ k ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT i = n NEW_LINE currSum = k NEW_LINE while ( i > 0 and currSum >= 0 ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ currSum ] ) : NEW_LINE INDENT i -= 1 NEW_LINE set2 . append ( arr [ i ] ) NEW_LINE DEDENT elif ( dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ) : NEW_LINE INDENT i -= 1 NEW_LINE currSum -= arr [ i ] NEW_LINE set1 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT print ( " Set ▁ 1 ▁ elements : " , end = " ▁ " ) NEW_LINE for i in range ( len ( set1 ) ) : NEW_LINE INDENT print ( set1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " Set 2 elements : " , ▁ end ▁ = ▁ "   " ) NEW_LINE for i in range ( len ( set2 ) ) : NEW_LINE INDENT print ( set2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 5 , 1 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE printEqualSumSets ( arr , n ) NEW_LINE DEDENT
Maximum average sum partition of an array | Python3 program for maximum average sum partition ; storing prefix sums ; for each i to n storing averages ; Driver code ; K = 3 ; atmost partitioning size
def largestSumOfAverages ( A , K ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE pre_sum = [ 0 ] * ( n + 1 ) ; NEW_LINE pre_sum [ 0 ] = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; NEW_LINE DEDENT dp = [ 0 ] * n ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) ; NEW_LINE DEDENT for k in range ( K - 1 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , ( pre_sum [ j ] - pre_sum [ i ] ) / ( j - i ) + dp [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT return int ( dp [ 0 ] ) ; NEW_LINE DEDENT A = [ 9 , 1 , 2 , 3 , 9 ] ; NEW_LINE print ( largestSumOfAverages ( A , K ) ) ; NEW_LINE
Find maximum sum array of length less than or equal to m | N and M to define sizes of arr , dp , current_arr and maxSum ; INF to define min value ; Function to find maximum sum ; dp array of size N x M ; current_arr of size M ; maxsum of size M ; If we have 0 elements from 0 th array ; Compute the cumulative sum array ; Calculating the maximum contiguous array for every length j , j is from 1 to lengtn of the array ; Every state is depending on its previous state ; computation of dp table similar approach as knapsack problem ; Now we have done processing with the last array lets find out what is the maximum sum possible ; Driver code ; First element of each row is the size of that row
N = 105 NEW_LINE M = 1001 NEW_LINE INF = - 1111111111 NEW_LINE def maxSum ( arr ) : NEW_LINE INDENT dp = [ [ - 1 for x in range ( M ) ] for y in range ( N ) ] NEW_LINE current_arr = [ 0 ] * M NEW_LINE maxsum = [ 0 ] * M NEW_LINE current_arr [ 0 ] = 0 NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , 6 ) : NEW_LINE INDENT len = arr [ i - 1 ] [ 0 ] NEW_LINE for j in range ( 1 , len + 1 ) : NEW_LINE INDENT current_arr [ j ] = arr [ i - 1 ] [ j ] NEW_LINE current_arr [ j ] += current_arr [ j - 1 ] NEW_LINE maxsum [ j ] = INF NEW_LINE DEDENT j = 1 NEW_LINE while j <= len and j <= 6 : NEW_LINE INDENT for k in range ( 1 , len + 1 ) : NEW_LINE INDENT if ( j + k - 1 <= len ) : NEW_LINE INDENT maxsum [ j ] = max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT for j in range ( 7 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT for j in range ( 1 , 7 ) : NEW_LINE INDENT cur = 1 NEW_LINE while cur <= j and cur <= len : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - cur ] + maxsum [ cur ] ) NEW_LINE cur += 1 NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 7 ) : NEW_LINE INDENT ans = max ( ans , dp [ 5 ] [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 3 , 2 , 3 , 5 ] , [ 2 , 7 , - 1 ] , [ 2 , 8 , 10 ] , [ 4 , 5 , 2 , 6 , 1 ] , [ 3 , 2 , 3 , - 2 ] ] NEW_LINE print ( " Maximum ▁ sum ▁ can ▁ be ▁ obtained " , " is ▁ : ▁ " , maxSum ( arr ) ) NEW_LINE DEDENT
Maximize array elements upto given number | Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index 1. dp [ i , j ] = 1 if value j can be obtained upto index i . 2. dp [ i , j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is True , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code
def findMaxVal ( arr , n , num , maxLimit ) : NEW_LINE INDENT ind = - 1 ; NEW_LINE val = - 1 ; NEW_LINE dp = [ [ 0 for i in range ( maxLimit + 1 ) ] for j in range ( n ) ] ; NEW_LINE for ind in range ( n ) : NEW_LINE INDENT for val in range ( maxLimit + 1 ) : NEW_LINE INDENT if ( ind == 0 ) : NEW_LINE INDENT if ( num - arr [ ind ] == val or num + arr [ ind ] == val ) : NEW_LINE INDENT dp [ ind ] [ val ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ ind ] [ val ] = 0 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( val - arr [ ind ] >= 0 and val + arr [ ind ] <= maxLimit ) : NEW_LINE INDENT if ( dp [ ind - 1 ] [ val - arr [ ind ] ] == 1 or dp [ ind - 1 ] [ val + arr [ ind ] ] == 1 ) : NEW_LINE INDENT dp [ ind ] [ val ] = 1 ; NEW_LINE DEDENT DEDENT elif ( val - arr [ ind ] >= 0 ) : NEW_LINE INDENT dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] ; NEW_LINE DEDENT elif ( val + arr [ ind ] <= maxLimit ) : NEW_LINE INDENT dp [ ind ] [ val ] = dp [ ind - 1 ] [ val + arr [ ind ] ] ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ ind ] [ val ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT for val in range ( maxLimit , - 1 , - 1 ) : NEW_LINE INDENT if ( dp [ n - 1 ] [ val ] == 1 ) : NEW_LINE INDENT return val ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 1 ; NEW_LINE arr = [ 3 , 10 , 6 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE maxLimit = 15 ; NEW_LINE print ( findMaxVal ( arr , n , num , maxLimit ) ) ; NEW_LINE DEDENT
Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Program
def gen ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n % 2 == 0 : NEW_LINE INDENT return 4 * gen ( n // 2 ) NEW_LINE DEDENT elif n % 2 == 1 : NEW_LINE INDENT return 4 * gen ( n // 2 ) + 1 NEW_LINE DEDENT DEDENT def moserDeBruijn ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( gen ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 15 NEW_LINE print ( " First " , n , " terms ▁ of ▁ " , " Moser - de ▁ Bruijn ▁ Sequence : " ) NEW_LINE moserDeBruijn ( n ) NEW_LINE
Minimum Sum Path in a Triangle | Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver Code
def minSumPath ( A ) : NEW_LINE INDENT memo = [ None ] * len ( A ) NEW_LINE n = len ( A ) - 1 NEW_LINE for i in range ( len ( A [ n ] ) ) : NEW_LINE INDENT memo [ i ] = A [ n ] [ i ] NEW_LINE DEDENT for i in range ( len ( A ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( len ( A [ i ] ) ) : NEW_LINE INDENT memo [ j ] = A [ i ] [ j ] + min ( memo [ j ] , memo [ j + 1 ] ) ; NEW_LINE DEDENT DEDENT return memo [ 0 ] NEW_LINE DEDENT A = [ [ 2 ] , [ 3 , 9 ] , [ 1 , 6 , 7 ] ] NEW_LINE print ( minSumPath ( A ) ) NEW_LINE
Check if any valid sequence is divisible by M |  ; Base case ; check if sum is divisible by M ; 1. Try placing '+ ; 2. Try placing '-
def isPossible ( index , sum ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT if ( ( sum % M ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; NEW_LINE DEDENT ' NEW_LINE INDENT placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; NEW_LINE if ( placeAdd or placeMinus ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT
Minimum removals from array to make max | Python program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver Code
MAX = 100 NEW_LINE dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE for i in range ( 0 , MAX ) : NEW_LINE INDENT for j in range ( 0 , MAX ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT def countRemovals ( a , i , j , k ) : NEW_LINE INDENT global dp NEW_LINE if ( i >= j ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( ( a [ j ] - a [ i ] ) <= k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT elif ( ( a [ j ] - a [ i ] ) > k ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 + min ( countRemovals ( a , i + 1 , j , k ) , countRemovals ( a , i , j - 1 , k ) ) NEW_LINE DEDENT return dp [ i ] [ j ] NEW_LINE DEDENT def removals ( a , n , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return countRemovals ( a , 0 , n - 1 , k ) NEW_LINE DEDENT DEDENT a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( removals ( a , n , k ) ) NEW_LINE
Minimum removals from array to make max | Function to find rightmost index which satisfies the condition arr [ j ] - arr [ i ] <= k ; Initialising start to i + 1 ; Initialising end to n - 1 ; Binary search implementation to find the rightmost element that satisfy the condition ; Check if the condition satisfies ; Store the position ; Make start = mid + 1 ; Make end = mid ; Return the rightmost position ; Function to calculate minimum number of elements to be removed ; Sort the given array ; Iterate from 0 to n - 1 ; Find j such that arr [ j ] - arr [ i ] <= k ; If there exist such j that satisfies the condition ; Number of elements to be removed for this particular case is ( j - i + 1 ) ; Return answer ; Driver Code
def findInd ( key , i , n , k , arr ) : NEW_LINE INDENT ind = - 1 NEW_LINE start = i + 1 NEW_LINE end = n - 1 ; NEW_LINE while ( start < end ) : NEW_LINE INDENT mid = int ( start + ( end - start ) / 2 ) NEW_LINE if ( arr [ mid ] - key <= k ) : NEW_LINE INDENT ind = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid NEW_LINE DEDENT DEDENT return ind NEW_LINE DEDENT def removals ( arr , n , k ) : NEW_LINE INDENT ans = n - 1 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT j = findInd ( arr [ i ] , i , n , k , arr ) NEW_LINE if ( j != - 1 ) : NEW_LINE INDENT ans = min ( ans , n - ( j - i + 1 ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( removals ( a , n , k ) ) NEW_LINE
Number of ordered pairs such that ( Ai & Aj ) = 0 | Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) are considered different ; Driver Code
def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( a [ i ] & a [ j ] ) == 0 : NEW_LINE INDENT count += 2 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT a = [ 3 , 4 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE
Number of ordered pairs such that ( Ai & Aj ) = 0 | Python program to calculate the number of ordered pairs such that their bitwise and is zero ; efficient function to count pairs ; stores the frequency of each number ; initialize 0 to all ; count the frequency of every element ; iterate for al possible values that a [ i ] can be ; if the last bit is ON ; else : is the last bit is OFF ; iterate till n ; if mask 's ith bit is set ; else : if mask 's ith bit is not set ; iterate for all the array element and count the number of pairs ; return answer ; Driver Code
N = 15 NEW_LINE def countPairs ( a , n ) : NEW_LINE INDENT Hash = { } NEW_LINE dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( 1 << N ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in Hash : NEW_LINE INDENT Hash [ a [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT Hash [ a [ i ] ] += 1 NEW_LINE DEDENT DEDENT mask = 0 NEW_LINE while ( mask < ( 1 << N ) ) : NEW_LINE INDENT if mask not in Hash : NEW_LINE INDENT Hash [ mask ] = 0 NEW_LINE DEDENT if ( mask & 1 ) : NEW_LINE INDENT dp [ mask ] [ 0 ] = Hash [ mask ] + Hash [ mask ^ 1 ] NEW_LINE dp [ mask ] [ 0 ] = Hash [ mask ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( mask & ( 1 << i ) ) : NEW_LINE INDENT dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] + dp [ mask ^ ( 1 << i ) ] [ i - 1 ] NEW_LINE dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] NEW_LINE DEDENT DEDENT mask += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] ] [ N ] NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 5 , 4 , 1 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE
Minimum sum of multiplications of n numbers | Python3 program to find the minimum sum of multiplication of n numbers ; Used in recursive memoized solution ; function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code
import numpy as np NEW_LINE import sys NEW_LINE dp = np . zeros ( ( 1000 , 1000 ) ) NEW_LINE def sum ( a , i , j ) : NEW_LINE INDENT ans = 0 NEW_LINE for m in range ( i , j + 1 ) : NEW_LINE INDENT ans = ( ans + a [ m ] ) % 100 NEW_LINE DEDENT return ans NEW_LINE DEDENT def solve ( a , i , j ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT dp [ i ] [ j ] = sys . maxsize NEW_LINE for k in range ( i , j ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , ( solve ( a , i , k ) + solve ( a , k + 1 , j ) + ( sum ( a , i , k ) * sum ( a , k + 1 , j ) ) ) ) NEW_LINE DEDENT return dp [ i ] [ j ] NEW_LINE DEDENT def initialize ( n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 40 , 60 , 20 ] NEW_LINE n = len ( a ) NEW_LINE initialize ( n ) NEW_LINE print ( int ( solve ( a , 0 , n - 1 ) ) ) NEW_LINE DEDENT
Print Fibonacci Series in reverse order | Python 3 Program to print Fibonacci series in reverse order ; assigning first and second elements ; storing sum in the preceding location ; printing array in reverse order ; Driver function
def reverseFibonacci ( n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE a [ 0 ] = 0 NEW_LINE a [ 1 ] = 1 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 2 ] + a [ i - 1 ] NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE reverseFibonacci ( n ) NEW_LINE