codeparrot/xlcost-text-to-code huggingface dataset:
Split a Numeric String into Fibonacci Sequence | Python3 program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver Code ; Given String ; Function Call
import sys NEW_LINE def splitIntoFibonacciHelper ( pos , S , seq ) : NEW_LINE INDENT if ( pos == len ( S ) and ( len ( seq ) >= 3 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( pos , len ( S ) ) : NEW_LINE INDENT num = num * 10 + ( ord ( S [ i ] ) - ord ( '0' ) ) NEW_LINE if ( num > sys . maxsize ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( ord ( S [ pos ] ) == ord ( '0' ) and i > pos ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( len ( seq ) > 2 and ( num > ( seq [ - 1 ] + seq [ len ( seq ) - 2 ] ) ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( len ( seq ) < 2 or ( num == ( seq [ - 1 ] + seq [ len ( seq ) - 2 ] ) ) ) : NEW_LINE INDENT seq . append ( num ) NEW_LINE if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT seq . pop ( ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def splitIntoFibonacci ( S ) : NEW_LINE INDENT seq = [ ] NEW_LINE splitIntoFibonacciHelper ( 0 , S , seq ) NEW_LINE if ( len ( seq ) >= 3 ) : NEW_LINE INDENT for i in seq : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 , end = ' ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "11235813" NEW_LINE splitIntoFibonacci ( S ) NEW_LINE DEDENT
Count pair of strings whose concatenation has every vowel | Function to return the count of all concatenated string with each vowel at least once ; Creating a hash array with initial value as 0 ; Traversing through each string and getting hash value for each of them ; Initializing the weight of each string ; Find the hash value for each string ; Increasing the count of the hash value ; Getting all possible pairs of indexes in hash array ; Check if the pair which has hash value 31 and multiplying the count of string and add it strCount ; Corner case , for strings which independently has all the vowels ; Return thre final count ; Given array of strings ;
def good_pairs ( Str , N ) : NEW_LINE INDENT arr = [ 0 for i in range ( 32 ) ] NEW_LINE strCount = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT Weight = 0 NEW_LINE for j in range ( len ( Str [ i ] ) ) : NEW_LINE INDENT switcher = { ' a ' : 1 , ' e ' : 2 , ' i ' : 4 , ' o ' : 8 , ' u ' : 16 , } NEW_LINE Weight = Weight | switcher . get ( Str [ i ] [ j ] , 0 ) NEW_LINE DEDENT arr [ Weight ] += 1 NEW_LINE DEDENT for i in range ( 32 ) : NEW_LINE INDENT for j in range ( i + 1 , 32 ) : NEW_LINE INDENT if ( ( i j ) == 31 ) : NEW_LINE INDENT strCount += arr [ i ] * arr [ j ] NEW_LINE DEDENT DEDENT DEDENT strCount += int ( ( arr [ 31 ] * ( arr [ 31 ] - 1 ) ) / 2 ) NEW_LINE return strCount NEW_LINE DEDENT Str = [ " aaweiolkju " , " oxdfgujkmi " ] NEW_LINE N = len ( Str ) NEW_LINE / * Function call * / NEW_LINE print ( good_pairs ( Str , N ) ) NEW_LINE
Color a grid such that all same color cells are connected either horizontally or vertically | Python3 program to color a grid such that all same color cells are connected either horizontally or vertically ; Current color ; Final grid ; If even row ; Traverse from left to right ; If color has been exhausted , move to the next color ; Color the grid at this position print ( i , j ) ; Reduce the color count ; Traverse from right to left for odd rows ; Print the grid ; Driver code
def solve ( arr , r , c ) : NEW_LINE INDENT idx = 1 NEW_LINE dp = [ [ 0 for i in range ( c ) ] for i in range ( r ) ] NEW_LINE for i in range ( r ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT if ( arr [ idx - 1 ] == 0 ) : NEW_LINE INDENT idx += 1 NEW_LINE DEDENT dp [ i ] [ j ] = idx NEW_LINE arr [ idx - 1 ] -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( c - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ idx - 1 ] == 0 ) : NEW_LINE INDENT idx += 1 NEW_LINE DEDENT dp [ i ] [ j ] = idx NEW_LINE arr [ idx - 1 ] -= 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT print ( dp [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 3 NEW_LINE c = 5 NEW_LINE n = 5 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE solve ( arr , r , c ) NEW_LINE DEDENT
Count of 0 s to be flipped to make any two adjacent 1 s at least K 0 s apart | Function to find the count of 0 s to be flipped ; Loop traversal to mark K adjacent positions to the right of already existing 1 s . ; Loop traversal to mark K adjacent positions to the left of already existing 1 s . ; Loop to count the maximum number of 0 s that will be replaced by 1 s ; Driver code
def count ( k , s ) : NEW_LINE INDENT ar = [ 0 ] * len ( s ) NEW_LINE end = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] == '1' : NEW_LINE INDENT for j in range ( i , len ( s ) ) : NEW_LINE INDENT if ( j <= i + k ) : NEW_LINE INDENT ar [ j ] = - 1 NEW_LINE end = j NEW_LINE DEDENT DEDENT i = end NEW_LINE DEDENT DEDENT end = 0 NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( j >= i - k ) : NEW_LINE INDENT ar [ j ] = - 1 NEW_LINE end = j NEW_LINE DEDENT DEDENT i = end NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE end = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT if ( ar [ j ] == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE for g in range ( j , len ( s ) ) : NEW_LINE INDENT if ( g <= j + k ) : NEW_LINE INDENT ar [ g ] = - 1 NEW_LINE end = g NEW_LINE DEDENT DEDENT j = end - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT K = 2 NEW_LINE s = "000000" NEW_LINE print ( count ( K , s ) ) NEW_LINE
Length of longest connected 1 â €™ s in a Binary Grid | Python3 program for the above approach ; Keeps a track of directions that is up , down , left , right ; Function to perform the dfs traversal ; Mark the current node as visited ; Increment length from this node ; Update the diameter length ; Move to next cell in x - direction ; Move to next cell in y - direction ; Check if cell is invalid then continue ; Perform DFS on new cell ; Decrement the length ; Function to find the maximum length of connected 1 s in the given grid ; Increment the id ; Traverse the grid [ ] ; Find start point of start dfs call ; DFS Traversal from cell ( x , y ) ; Print the maximum length ; Driver Code ; Given grid [ ] [ ] ; Function Call
row = 6 NEW_LINE col = 7 NEW_LINE vis = [ [ 0 for i in range ( col + 1 ) ] for j in range ( row + 1 ) ] NEW_LINE id = 0 NEW_LINE diameter = 0 NEW_LINE length = 0 NEW_LINE dx = [ - 1 , 1 , 0 , 0 ] NEW_LINE dy = [ 0 , 0 , - 1 , 1 ] NEW_LINE def dfs ( a , b , lis , x , y ) : NEW_LINE INDENT global id , length , diameter NEW_LINE vis [ a ] [ b ] = id NEW_LINE length += 1 NEW_LINE if ( length > diameter ) : NEW_LINE INDENT x = a NEW_LINE y = b NEW_LINE diameter = length NEW_LINE DEDENT for j in range ( 4 ) : NEW_LINE INDENT cx = a + dx [ j ] NEW_LINE cy = b + dy [ j ] NEW_LINE if ( cx < 0 or cy < 0 or cx >= row or cy >= col or lis [ cx ] [ cy ] == 0 or vis [ cx ] [ cy ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( cx , cy , lis , x , y ) NEW_LINE DEDENT vis [ a ] [ b ] = 0 NEW_LINE length -= 1 NEW_LINE return x , y NEW_LINE DEDENT def findMaximumLength ( lis ) : NEW_LINE INDENT global id , length , diameter NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE id += 1 NEW_LINE length = 0 NEW_LINE diameter = 0 NEW_LINE for i in range ( row ) : NEW_LINE INDENT for j in range ( col ) : NEW_LINE INDENT if ( lis [ i ] [ j ] != 0 ) : NEW_LINE INDENT x , y = dfs ( i , j , lis , x , y ) NEW_LINE i = row NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT id += 1 NEW_LINE length = 0 NEW_LINE diameter = 0 NEW_LINE x , y = dfs ( x , y , lis , x , y ) NEW_LINE print ( diameter ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT grid = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ] NEW_LINE findMaximumLength ( grid ) NEW_LINE DEDENT
Last two digits of powers of 7 | Function to find the last two digits of 7 ^ N ; Case 4 ; Case 3 ; Case 2 ; Case 1 ; Given number ; Function call
def get_last_two_digit ( N ) : NEW_LINE INDENT if ( N % 4 == 0 ) : NEW_LINE INDENT return "01" ; NEW_LINE DEDENT elif ( N % 4 == 1 ) : NEW_LINE INDENT return "07" ; NEW_LINE DEDENT elif ( N % 4 == 2 ) : NEW_LINE INDENT return "49" ; NEW_LINE DEDENT return "43" ; NEW_LINE DEDENT N = 12 ; NEW_LINE print ( get_last_two_digit ( N ) ) NEW_LINE
Subsequence with maximum pairwise absolute difference and minimum size | Function to find the subsequence with maximum absolute difference ; To store the resultant subsequence ; First element should be included in the subsequence ; Traverse the given array arr [ ] ; If current element is greater than the previous element ; If the current element is not the local maxima then continue ; Else push it in subsequence ; If the current element is less then the previous element ; If the current element is not the local minima then continue ; Else push it in subsequence ; Last element should also be included in subsequence ; Print the element ; Driver Code ; Given array ; Function Call
def getSubsequence ( ar ) : NEW_LINE INDENT N = len ( ar ) NEW_LINE ans = [ ] NEW_LINE ans . append ( ar [ 0 ] ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT if ( ar [ i ] > ar [ i - 1 ] ) : NEW_LINE INDENT if ( i < N - 1 and ar [ i ] <= ar [ i + 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( ar [ i ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( i < N - 1 and ar [ i + 1 ] < ar [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( ar [ i ] ) NEW_LINE DEDENT DEDENT DEDENT ans . append ( ar [ N - 1 ] ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 3 , 5 ] NEW_LINE getSubsequence ( arr ) NEW_LINE DEDENT
Maximum product of two non | Returns maximum length path in subtree rooted at u after removing edge connecting u and v ; To find lengths of first and second maximum in subtrees . currMax is to store overall maximum . ; loop through all neighbors of u ; if neighbor is v , then skip it ; call recursively with current neighbor as root ; get max from one side and update ; store total length by adding max and second max ; update current max by adding 1 , i . e . current node is included ; method returns maximum product of length of two non - intersecting paths ; one by one removing all edges and calling dfs on both subtrees ; calling dfs on subtree rooted at g [ i ] [ j ] , excluding edge from g [ i ] [ j ] to i . ; calling dfs on subtree rooted at i , edge from i to g [ i ] [ j ] ; Utility function to add an undirected edge ( u , v ) ; Driver code ; there are N edges , so + 1 for nodes and + 1 for 1 - based indexing
def dfs ( g , curMax , u , v ) : NEW_LINE INDENT max1 = 0 NEW_LINE max2 = 0 NEW_LINE total = 0 NEW_LINE for i in range ( len ( g [ u ] ) ) : NEW_LINE INDENT if ( g [ u ] [ i ] == v ) : NEW_LINE INDENT continue NEW_LINE DEDENT total = max ( total , dfs ( g , curMax , g [ u ] [ i ] , u ) ) NEW_LINE if ( curMax [ 0 ] > max1 ) : NEW_LINE INDENT max2 = max1 NEW_LINE max1 = curMax [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT max2 = max ( max2 , curMax [ 0 ] ) NEW_LINE DEDENT DEDENT total = max ( total , max1 + max2 ) NEW_LINE curMax [ 0 ] = max1 + 1 NEW_LINE return total NEW_LINE DEDENT def maxProductOfTwoPaths ( g , N ) : NEW_LINE INDENT res = - 999999999999 NEW_LINE path1 , path2 = None , None NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( len ( g [ i ] ) ) : NEW_LINE INDENT curMax = [ 0 ] NEW_LINE path1 = dfs ( g , curMax , g [ i ] [ j ] , i ) NEW_LINE curMax = [ 0 ] NEW_LINE path2 = dfs ( g , curMax , i , g [ i ] [ j ] ) NEW_LINE res = max ( res , path1 * path2 ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def addEdge ( g , u , v ) : NEW_LINE INDENT g [ u ] . append ( v ) NEW_LINE g [ v ] . append ( u ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 1 , 8 ] , [ 2 , 6 ] , [ 3 , 1 ] , [ 5 , 3 ] , [ 7 , 8 ] , [ 8 , 4 ] , [ 8 , 6 ] ] NEW_LINE N = len ( edges ) NEW_LINE g = [ [ ] for i in range ( N + 2 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT addEdge ( g , edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) NEW_LINE DEDENT print ( maxProductOfTwoPaths ( g , N ) ) NEW_LINE DEDENT
Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
def Minsteps ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT if ( n % 5 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 5 NEW_LINE continue NEW_LINE DEDENT elif ( n % 4 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 4 NEW_LINE continue NEW_LINE DEDENT elif ( n % 3 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 3 NEW_LINE continue NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE n = n / 2 NEW_LINE continue NEW_LINE DEDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 10 NEW_LINE print ( Minsteps ( n ) ) NEW_LINE
Check if the square of a number is divisible by K or not | Python3 implementation to check if the square of X is divisible by K ; Function to return if square of X is divisible by K ; Finding gcd of x and k ; Dividing k by their gcd ; Check for divisibility of X by reduced K ; Driver Code
from math import gcd NEW_LINE def checkDivisible ( x , k ) : NEW_LINE INDENT g = gcd ( x , k ) NEW_LINE k //= g NEW_LINE if ( x % k == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 6 NEW_LINE k = 9 NEW_LINE checkDivisible ( x , k ) ; NEW_LINE DEDENT
Find the minimum value of the given expression over all pairs of the array | Python3 program to find the minimum value of the given expression over all pairs of the array ; Function to find the minimum value of the expression ; The expression simplifies to finding the minimum xor value pair Sort given array ; Calculate min xor of consecutive pairs ; Driver code
import sys NEW_LINE def MinimumValue ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE minXor = sys . maxsize ; NEW_LINE val = 0 ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT val = arr [ i ] ^ arr [ i + 1 ] ; NEW_LINE minXor = min ( minXor , val ) ; NEW_LINE DEDENT return minXor ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE A = [ 12 , 3 , 14 , 5 , 9 , 8 ] ; NEW_LINE print ( MinimumValue ( A , N ) ) ; NEW_LINE DEDENT
Find length of longest substring with at most K normal characters | Function to find maximum length of normal substrings ; keeps count of normal characters ; indexes of substring ; maintain length of longest substring with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with substring length ; get position of character ; check if character is normal then decrement count ; Driver code ; initialise the string
def maxNormalSubstring ( P , Q , K , N ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE left , right = 0 , 0 NEW_LINE ans = 0 NEW_LINE while ( right < N ) : NEW_LINE INDENT while ( right < N and count <= K ) : NEW_LINE INDENT pos = ord ( P [ right ] ) - ord ( ' a ' ) NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT if ( count + 1 > K ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT right += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT ans = max ( ans , right - left ) NEW_LINE DEDENT DEDENT while ( left < right ) : NEW_LINE INDENT pos = ord ( P [ left ] ) - ord ( ' a ' ) NEW_LINE left += 1 NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( count < K ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if ( __name__ == " _ _ main _ _ " ) : NEW_LINE INDENT P = " giraffe " NEW_LINE Q = "01111001111111111011111111" NEW_LINE K = 2 NEW_LINE N = len ( P ) NEW_LINE print ( maxNormalSubstring ( P , Q , K , N ) ) NEW_LINE DEDENT
Make all the elements of array even with given operations | Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code
def countOperations ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT arr [ i ] += 1 ; NEW_LINE arr [ i + 1 ] += 1 ; NEW_LINE count += 2 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countOperations ( arr , n ) ) ; NEW_LINE DEDENT
Count of pairs with difference at most K with no element repeating | Function to count the number of pairs whose difference is atmost K in an array ; Sorting the Array ; Variable to store the count of pairs whose difference is atmost K ; Loop to consider the consecutive pairs of the array ; if Pair found increment the index by 2 ; Driver Code ; Function Call
def countPairs ( arr , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE pair = 0 NEW_LINE index = 0 NEW_LINE while ( index < len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ index + 1 ] - arr [ index ] <= k : NEW_LINE INDENT pair += 1 NEW_LINE index += 2 NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT return pair NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 7 , 5 ] NEW_LINE k = 2 NEW_LINE count = countPairs ( arr , k ) NEW_LINE print ( count ) NEW_LINE DEDENT
Maximum profit by selling N items at two markets | Function to calculate max profit ; Prefix sum array for profitA [ ] ; Suffix sum array for profitB [ ] ; If all the items are sold in market A ; Find the maximum profit when the first i items are sold in market A and the rest of the items are sold in market B for all possible values of i ; If all the items are sold in market B ; Driver code ; Function to calculate max profit
def maxProfit ( profitA , profitB , n ) : NEW_LINE INDENT preSum = [ 0 ] * n ; NEW_LINE preSum [ 0 ] = profitA [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; NEW_LINE DEDENT suffSum = [ 0 ] * n ; NEW_LINE suffSum [ n - 1 ] = profitB [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; NEW_LINE DEDENT res = preSum [ n - 1 ] ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT res = max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; NEW_LINE DEDENT res = max ( res , suffSum [ 0 ] ) ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT profitA = [ 2 , 3 , 2 ] ; NEW_LINE profitB = [ 10 , 30 , 40 ] ; NEW_LINE n = len ( profitA ) ; NEW_LINE print ( maxProfit ( profitA , profitB , n ) ) ; NEW_LINE DEDENT
Minimum number of edges that need to be added to form a triangle | Function to return the minimum number of edges that need to be added to the given graph such that it contains at least one triangle ; adj is the adjacency matrix such that adj [ i ] [ j ] = 1 when there is an edge between i and j ; As the graph is undirected so there will be an edge between ( i , j ) and ( j , i ) ; To store the required count of edges ; For every possible vertex triplet ; If the vertices form a triangle ; If no edges are present ; If only 1 edge is required ; Two edges are required ; Driver code ; Number of nodes ; Storing the edges in a vector of pairs
def minEdges ( v , n ) : NEW_LINE INDENT adj = dict . fromkeys ( range ( n + 1 ) ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT adj [ i ] = [ 0 ] * ( n + 1 ) ; NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT adj [ v [ i ] [ 0 ] ] [ v [ i ] [ 1 ] ] = 1 ; NEW_LINE adj [ v [ i ] [ 1 ] ] [ v [ i ] [ 0 ] ] = 1 ; NEW_LINE DEDENT edgesNeeded = 3 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n + 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n + 1 ) : NEW_LINE INDENT if ( adj [ i ] [ j ] and adj [ j ] [ k ] and adj [ k ] [ i ] ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( not ( adj [ i ] [ j ] or adj [ j ] [ k ] or adj [ k ] [ i ] ) ) : NEW_LINE INDENT edgesNeeded = min ( edgesNeeded , 3 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( adj [ i ] [ j ] and adj [ j ] [ k ] ) or ( adj [ j ] [ k ] and adj [ k ] [ i ] ) or ( adj [ k ] [ i ] and adj [ i ] [ j ] ) ) : NEW_LINE INDENT edgesNeeded = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT edgesNeeded = min ( edgesNeeded , 2 ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return edgesNeeded ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE v = [ [ 1 , 2 ] , [ 1 , 3 ] ] ; NEW_LINE print ( minEdges ( v , n ) ) ; NEW_LINE DEDENT
Length of the longest subsequence such that xor of adjacent elements is non | Function to find the length of the longest subsequence such that the XOR of adjacent elements in the subsequence must be non - decreasing ; Computing xor of all the pairs of elements and store them along with the pair ( i , j ) ; Sort all possible xor values ; Initialize the dp array ; Calculating the dp array for each possible position and calculating the max length that ends at a particular index ; Taking maximum of all position ; Driver code
def LongestXorSubsequence ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT v . append ( [ ( arr [ i ] ^ arr [ j ] ) , ( i , j ) ] ) NEW_LINE DEDENT DEDENT v . sort ( ) NEW_LINE dp = [ 1 for x in range ( 88 ) ] NEW_LINE for a , b in v : NEW_LINE INDENT dp [ b [ 1 ] ] = max ( dp [ b [ 1 ] ] , 1 + dp [ b [ 0 ] ] ) NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 12 , 6 , 7 , 13 , 14 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( LongestXorSubsequence ( arr , n ) ) NEW_LINE