codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver program to test above function
def smallestSubWithSum ( arr , n , x ) : NEW_LINE INDENT min_len = n + 1 NEW_LINE for start in range ( 0 , n ) : NEW_LINE INDENT curr_sum = arr [ start ] NEW_LINE if ( curr_sum > x ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT for end in range ( start + 1 , n ) : NEW_LINE INDENT curr_sum += arr [ end ] NEW_LINE if curr_sum > x and ( end - start + 1 ) < min_len : NEW_LINE INDENT min_len = ( end - start + 1 ) NEW_LINE DEDENT DEDENT DEDENT return min_len ; NEW_LINE DEDENT arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ] NEW_LINE x = 51 NEW_LINE n1 = len ( arr1 ) NEW_LINE res1 = smallestSubWithSum ( arr1 , n1 , x ) ; NEW_LINE if res1 == n1 + 1 : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res1 ) NEW_LINE DEDENT arr2 = [ 1 , 10 , 5 , 2 , 7 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE x = 9 NEW_LINE res2 = smallestSubWithSum ( arr2 , n2 , x ) ; NEW_LINE if res2 == n2 + 1 : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res2 ) NEW_LINE DEDENT arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE x = 280 NEW_LINE res3 = smallestSubWithSum ( arr3 , n3 , x ) NEW_LINE if res3 == n3 + 1 : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res3 ) NEW_LINE DEDENT
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver program
def smallestSubWithSum ( arr , n , x ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE min_len = n + 1 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT while ( curr_sum <= x and end < n ) : NEW_LINE INDENT curr_sum += arr [ end ] NEW_LINE end += 1 NEW_LINE DEDENT while ( curr_sum > x and start < n ) : NEW_LINE INDENT if ( end - start < min_len ) : NEW_LINE INDENT min_len = end - start NEW_LINE DEDENT curr_sum -= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT DEDENT return min_len NEW_LINE DEDENT arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ] NEW_LINE x = 51 NEW_LINE n1 = len ( arr1 ) NEW_LINE res1 = smallestSubWithSum ( arr1 , n1 , x ) NEW_LINE print ( " Not ▁ possible " ) if ( res1 == n1 + 1 ) else print ( res1 ) NEW_LINE arr2 = [ 1 , 10 , 5 , 2 , 7 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE x = 9 NEW_LINE res2 = smallestSubWithSum ( arr2 , n2 , x ) NEW_LINE print ( " Not ▁ possible " ) if ( res2 == n2 + 1 ) else print ( res2 ) NEW_LINE arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE x = 280 NEW_LINE res3 = smallestSubWithSum ( arr3 , n3 , x ) NEW_LINE print ( " Not ▁ possible " ) if ( res3 == n3 + 1 ) else print ( res3 ) NEW_LINE
Sum of nodes on the longest path from root to leaf node | function to get a new node ; put in the data ; function to find the Sum of nodes on the longest path from root to leaf node ; if true , then we have traversed a root to leaf path ; update maximum Length and maximum Sum according to the given conditions ; recur for left subtree ; recur for right subtree ; utility function to find the Sum of nodes on the longest path from root to leaf node ; if tree is NULL , then Sum is 0 ; finding the maximum Sum ' maxSum ' for the maximum Length root to leaf path ; required maximum Sum ; Driver Code ; binary tree formation 4 ; / \ ; 2 5 ; / \ / \ ; 7 1 2 3 ; / ; 6
class getNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def SumOfLongRootToLeafPath ( root , Sum , Len , maxLen , maxSum ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT if ( maxLen [ 0 ] < Len ) : NEW_LINE INDENT maxLen [ 0 ] = Len NEW_LINE maxSum [ 0 ] = Sum NEW_LINE DEDENT elif ( maxLen [ 0 ] == Len and maxSum [ 0 ] < Sum ) : NEW_LINE INDENT maxSum [ 0 ] = Sum NEW_LINE DEDENT return NEW_LINE DEDENT SumOfLongRootToLeafPath ( root . left , Sum + root . data , Len + 1 , maxLen , maxSum ) NEW_LINE SumOfLongRootToLeafPath ( root . right , Sum + root . data , Len + 1 , maxLen , maxSum ) NEW_LINE DEDENT def SumOfLongRootToLeafPathUtil ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT maxSum = [ - 999999999999 ] NEW_LINE maxLen = [ 0 ] NEW_LINE SumOfLongRootToLeafPath ( root , 0 , 0 , maxLen , maxSum ) NEW_LINE return maxSum [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( 4 ) NEW_LINE root . left = getNode ( 2 ) NEW_LINE root . right = getNode ( 5 ) NEW_LINE root . left . left = getNode ( 7 ) NEW_LINE root . left . right = getNode ( 1 ) NEW_LINE root . right . left = getNode ( 2 ) NEW_LINE root . right . right = getNode ( 3 ) NEW_LINE root . left . right . left = getNode ( 6 ) NEW_LINE print ( " Sum ▁ = ▁ " , SumOfLongRootToLeafPathUtil ( root ) ) NEW_LINE DEDENT
Count L | Function to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
def numberOfArrays ( n , l ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( l + 1 ) ] NEW_LINE dp [ 0 ] [ 1 ] = 1 NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT for k in range ( j , n + 1 , j ) : NEW_LINE INDENT dp [ i + 1 ] [ k ] += dp [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans += dp [ l ] [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , L = 2 , 4 NEW_LINE print ( numberOfArrays ( N , L ) ) NEW_LINE DEDENT
Count minimum steps to get the given desired array | Returns count of minimum operations to convert a zero array to target array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert target to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of target don 't become 0. ; To store count of zeroes in current target array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code
def countMinOperations ( target , n ) : NEW_LINE INDENT result = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT zero_count = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( ( target [ i ] & 1 ) > 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT elif ( target [ i ] == 0 ) : NEW_LINE INDENT zero_count += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( zero_count == n ) : NEW_LINE INDENT return result ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT target [ j ] = target [ j ] // 2 ; NEW_LINE DEDENT result += 1 ; NEW_LINE DEDENT for j in range ( i , n ) : NEW_LINE INDENT if ( target [ j ] & 1 ) : NEW_LINE INDENT target [ j ] -= 1 ; NEW_LINE result += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 16 , 16 , 16 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to " , 	 	 " get the given target array is " , countMinOperations ( arr , n ) ) ; NEW_LINE
Maximize sum of odd | Function to find the maximum sum of array elements chosen by Player A according to the given criteria ; Store the key ; Corner Case ; Check if all the elements can be taken or not ; If the difference is less than or equal to the available chances then pick all numbers ; Find the sum of array elements over the range [ start , N ] ; If yes then return that value ; Traverse over the range [ 1 , 2 * M ] ; Sum of elements for Player A ; Even chance sum can be obtained by subtracting the odd chances sum - total and picking up the maximum from that ; Storing the value in dictionary ; Return the maximum sum of odd chances ; Driver Code ; Stores the precomputed values ; Function Call
def recursiveChoosing ( arr , start , M , dp ) : NEW_LINE INDENT key = ( start , M ) NEW_LINE if start >= N : NEW_LINE INDENT return 0 NEW_LINE DEDENT if N - start <= 2 * M : NEW_LINE INDENT return sum ( arr [ start : ] ) NEW_LINE DEDENT psa = 0 NEW_LINE total = sum ( arr [ start : ] ) NEW_LINE if key in dp : NEW_LINE INDENT return dp [ key ] NEW_LINE DEDENT for x in range ( 1 , 2 * M + 1 ) : NEW_LINE INDENT psb = recursiveChoosing ( arr , start + x , max ( x , M ) , dp ) NEW_LINE psa = max ( psa , total - psb ) NEW_LINE DEDENT dp [ key ] = psa NEW_LINE return dp [ key ] NEW_LINE DEDENT arr = [ 2 , 7 , 9 , 4 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE dp = { } NEW_LINE print ( recursiveChoosing ( arr , 0 , 1 , dp ) ) NEW_LINE
Number of subsets with product less than k | Python3 to find the count subset having product less than k ; declare four vector for dividing array into two halves and storing product value of possible subsets for them ; ignore element greater than k and divide array into 2 halves ; ignore element if greater than k ; generate all subsets for 1 st half ( vect1 ) ; push only in case subset product is less than equal to k ; generate all subsets for 2 nd half ( vect2 ) ; push only in case subset product is less than equal to k ; sort subset2 ; for null subset decrement the value of count ; return count ; Driver Code
import bisect NEW_LINE def findSubset ( arr , n , k ) : NEW_LINE INDENT vect1 , vect2 , subset1 , subset2 = [ ] , [ ] , [ ] , [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] > k : NEW_LINE INDENT continue NEW_LINE DEDENT if i <= n // 2 : NEW_LINE INDENT vect1 . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT vect2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( 0 , ( 1 << len ( vect1 ) ) ) : NEW_LINE INDENT value = 1 NEW_LINE for j in range ( 0 , len ( vect1 ) ) : NEW_LINE INDENT if i & ( 1 << j ) : NEW_LINE INDENT value *= vect1 [ j ] NEW_LINE DEDENT DEDENT if value <= k : NEW_LINE INDENT subset1 . append ( value ) NEW_LINE DEDENT DEDENT for i in range ( 0 , ( 1 << len ( vect2 ) ) ) : NEW_LINE INDENT value = 1 NEW_LINE for j in range ( 0 , len ( vect2 ) ) : NEW_LINE INDENT if i & ( 1 << j ) : NEW_LINE INDENT value *= vect2 [ j ] NEW_LINE DEDENT DEDENT if value <= k : NEW_LINE INDENT subset2 . append ( value ) NEW_LINE DEDENT DEDENT subset2 . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( subset1 ) ) : NEW_LINE INDENT count += bisect . bisect ( subset2 , ( k // subset1 [ i ] ) ) NEW_LINE DEDENT count -= 1 NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 3 , 6 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 25 NEW_LINE print ( findSubset ( arr , n , k ) ) NEW_LINE DEDENT
Maximum Sum Subsequence made up of consecutive elements of different parity | Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Base Case ; Store the parity of number at the ith position ; If the dp state has already been calculated , return it ; If the array is traversed and no element has been selected yet then select the current element ; If the parity of the current and previously selected element are different , then select the current element ; Skip the current element and move to the next element ; Return the result ; Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Initialize the dp [ ] array with - 1 ; Initially the prev value is set to say 2 , as the first element can anyways be selected ; Driver Code
def maxSum ( arr , i , n , prev , is_selected , dp ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT cur = abs ( arr [ i ] ) % 2 NEW_LINE if ( dp [ i ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ prev ] NEW_LINE DEDENT if ( i == n - 1 and is_selected == 0 ) : NEW_LINE INDENT dp [ i ] [ prev ] = arr [ i ] NEW_LINE return dp [ i ] [ prev ] NEW_LINE DEDENT if ( cur != prev ) : NEW_LINE INDENT dp [ i ] [ prev ] = arr [ i ] + maxSum ( arr , i + 1 , n , cur , 1 , dp ) NEW_LINE DEDENT dp [ i ] [ prev ] = max ( dp [ i ] [ prev ] , maxSum ( arr , i + 1 , n , prev , is_selected , dp ) ) NEW_LINE return dp [ i ] [ prev ] NEW_LINE DEDENT def maxSumUtil ( arr , n ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 3 ) ] for j in range ( 100 ) ] NEW_LINE print ( maxSum ( arr , 0 , n , 2 , 0 , dp ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 6 , 8 , - 5 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE maxSumUtil ( arr , N ) NEW_LINE DEDENT
Find minimum number of merge operations to make an array palindrome | Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver program to test above
def findMinOps ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE i , j = 0 , n - 1 NEW_LINE while i <= j : NEW_LINE INDENT if arr [ i ] == arr [ j ] : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT elif arr [ i ] > arr [ j ] : NEW_LINE INDENT j -= 1 NEW_LINE arr [ j ] += arr [ j + 1 ] NEW_LINE ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE arr [ i ] += arr [ i - 1 ] NEW_LINE ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 4 , 5 , 9 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + str ( findMinOps ( arr , n ) ) ) NEW_LINE
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver program to test above function
def findSmallest ( arr , n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] <= res : NEW_LINE INDENT res = res + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr1 = [ 1 , 3 , 4 , 5 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( findSmallest ( arr1 , n1 ) ) NEW_LINE arr2 = [ 1 , 2 , 6 , 10 , 11 , 15 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( findSmallest ( arr2 , n2 ) ) NEW_LINE arr3 = [ 1 , 1 , 1 , 1 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE print ( findSmallest ( arr3 , n3 ) ) NEW_LINE arr4 = [ 1 , 1 , 3 , 4 ] NEW_LINE n4 = len ( arr4 ) NEW_LINE print ( findSmallest ( arr4 , n4 ) ) NEW_LINE
Count distinct possible Bitwise XOR values of subsets of an array | Stores the Bitwise XOR of every possible subset ; Function to generate all combinations of subsets and store their Bitwise XOR in set S ; If the end of the subset is reached ; Stores the Bitwise XOR of the current subset ; Iterate comb [ ] to find XOR ; Insert the Bitwise XOR of R elements ; Otherwise , iterate to generate all possible subsets ; Recursive call for next index ; Function to find the size of the set having Bitwise XOR of all the subsets of the given array ; Iterate ove the given array ; Generate all possible subsets ; Print the size of the set ; Driver Code ; Function Call
s = set ( [ ] ) NEW_LINE def countXOR ( arr , comb , start , end , index , r ) : NEW_LINE INDENT if ( index == r ) : NEW_LINE INDENT new_xor = 0 NEW_LINE for j in range ( r ) : NEW_LINE INDENT new_xor ^= comb [ j ] NEW_LINE DEDENT s . add ( new_xor ) NEW_LINE return NEW_LINE DEDENT i = start NEW_LINE while i <= end and ( end - i + 1 ) >= ( r - index ) : NEW_LINE INDENT comb [ index ] = arr [ i ] NEW_LINE countXOR ( arr , comb , i + 1 , end , index + 1 , r ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def maxSizeSet ( arr , N ) : NEW_LINE INDENT for r in range ( 2 , N + 1 ) : NEW_LINE INDENT comb = [ 0 ] * ( r + 1 ) NEW_LINE countXOR ( arr , comb , 0 , N - 1 , 0 , r ) NEW_LINE DEDENT print ( len ( s ) ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE maxSizeSet ( arr , N ) NEW_LINE
Size of The Subarray With Maximum Sum | Python3 program to print largest contiguous array sum ; Function to find the maximum contiguous subarray and print its starting and end index ; Driver program to test maxSubArraySum
from sys import maxsize NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - maxsize - 1 NEW_LINE max_ending_here = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE s = 0 NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT max_ending_here += a [ i ] NEW_LINE if max_so_far < max_ending_here : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE start = s NEW_LINE end = i NEW_LINE DEDENT if max_ending_here < 0 : NEW_LINE INDENT max_ending_here = 0 NEW_LINE s = i + 1 NEW_LINE DEDENT DEDENT return ( end - start + 1 ) NEW_LINE DEDENT a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE print ( maxSubArraySum ( a , len ( a ) ) ) NEW_LINE
Longest subarray of an array which is a subsequence in another array | Function to find the length of the longest subarray in arr1 which is a subsequence in arr2 ; Length of the array arr1 ; Length of the array arr2 ; Length of the required longest subarray ; Initialize DParray ; Traverse array arr1 ; Traverse array arr2 ; arr1 [ i - 1 ] contributes to the length of the subarray ; Otherwise ; Find the maximum value present in DP ; Return the result ; Driver Code ; Function call to find the length of the longest required subarray
def LongSubarrSeq ( arr1 , arr2 ) : NEW_LINE INDENT M = len ( arr1 ) ; NEW_LINE N = len ( arr2 ) ; NEW_LINE maxL = 0 ; NEW_LINE DP = [ [ 0 for i in range ( N + 1 ) ] for j in range ( M + 1 ) ] ; NEW_LINE for i in range ( 1 , M + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( arr1 [ i - 1 ] == arr2 [ j - 1 ] ) : NEW_LINE INDENT DP [ i ] [ j ] = 1 + DP [ i - 1 ] [ j - 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i ] [ j - 1 ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( M + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT maxL = max ( maxL , DP [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return maxL ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 4 , 2 , 3 , 1 , 5 , 6 ] ; NEW_LINE arr2 = [ 3 , 1 , 4 , 6 , 5 , 2 ] ; NEW_LINE print ( LongSubarrSeq ( arr1 , arr2 ) ) ; NEW_LINE DEDENT
Find minimum difference between any two elements | Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver code
def findMinDiff ( arr , n ) : NEW_LINE INDENT diff = 10 ** 20 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if abs ( arr [ i ] - arr [ j ] ) < diff : NEW_LINE INDENT diff = abs ( arr [ i ] - arr [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return diff NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 19 , 18 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ difference ▁ is ▁ " + str ( findMinDiff ( arr , n ) ) ) NEW_LINE
Count possible N | DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the final count ; Print the total count ; Driver Code ; Function Call
dp = [ [ [ - 1 for i in range ( 5005 ) ] for i in range ( 12 ) ] for i in range ( 12 ) ] NEW_LINE def findCountUtil ( N , maxDigit , position , previous , count ) : NEW_LINE INDENT global dp NEW_LINE if ( position == N ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT ans = dp [ position ] [ previous ] [ count ] NEW_LINE if ( ans != - 1 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( count == 0 and previous != i ) : NEW_LINE INDENT ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % 1000000007 ) % 1000000007 NEW_LINE DEDENT elif ( count != 0 ) : NEW_LINE INDENT ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , count - 1 if ( previous == i and position != 0 ) else maxDigit [ i ] - 1 ) ) % 1000000007 ) % 1000000007 NEW_LINE DEDENT DEDENT dp [ position ] [ previous ] [ count ] = ans NEW_LINE return ans NEW_LINE DEDENT def findCount ( N , maxDigit ) : NEW_LINE INDENT ans = findCountUtil ( N , maxDigit , 0 , 0 , 1 ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE maxDigit = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE findCount ( N , maxDigit ) NEW_LINE DEDENT
Find minimum difference between any two elements | Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver code
def findMinDiff ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE diff = 10 ** 20 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if arr [ i + 1 ] - arr [ i ] < diff : NEW_LINE INDENT diff = arr [ i + 1 ] - arr [ i ] NEW_LINE DEDENT DEDENT return diff NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 19 , 18 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ difference ▁ is ▁ " + str ( findMinDiff ( arr , n ) ) ) NEW_LINE
Space optimization using bit manipulations | Python3 program to mark numbers as multiple of 2 or 5 ; Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
import math NEW_LINE a = 2 NEW_LINE b = 10 NEW_LINE size = abs ( b - a ) + 1 NEW_LINE array = [ 0 ] * size NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 or i % 5 == 0 ) : NEW_LINE INDENT array [ i - a ] = 1 NEW_LINE DEDENT DEDENT print ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if ( array [ i - a ] == 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT